รับค่าข้อความจาก Microsoft word ด้วย jsp
หน้าแรก JSP รับค่าข้อความจาก Microsoft word ด้วย jsp
1. ให้ทำการ import Library จาก http://poi.apache.org/ ลงใน Netbeans
2. ให้ทำการ สร้าง java class แล้วของ run ด้วย Code ด้านล่างนี้ดู
import java.io.*;
import java.util.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.*;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class TableTest {
public static void main (String[] args) throws Exception {
String fileName = "D:testwebUB.doc";
if (args.length > 0) {
fileName = args[0];
}
InputStream fis = new FileInputStream(fileName);
POIFSFileSystem fs = new POIFSFileSystem(fis);
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
for (int i=0; i<range.numParagraphs(); i++) {
Paragraph par = range.getParagraph(i);
System.out.println("paragraph "+(i+1));
System.out.println("is in table: "+par.isInTable());
System.out.println("is table row end: "+par.isTableRowEnd());
System.out.println(par.text());
}
Paragraph tablePar = range.getParagraph(0);
if (tablePar.isInTable()) {
Table table = range.getTable(tablePar);
for (int rowIdx=0; rowIdx<table.numRows(); rowIdx++) {
TableRow row = table.getRow(rowIdx);
System.out.println("row "+(rowIdx+1)+", is table header: "+row.isTableHeader());
for (int colIdx=0; colIdx<row.numCells(); colIdx++) {
TableCell cell = row.getCell(colIdx);
System.out.println("column "+(colIdx+1)+", text="+cell.getParagraph(0).text());
}
}
}
}
}
หรือ 3. Run โดยใช้ JSP ด้วย Code นี้
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.*,java.util.*, javax.servlet.*" %>
<%@ page import="org.apache.poi.hwpf.HWPFDocument,org.apache.poi.hwpf.model.*, javax.servlet.*, org.apache.poi.hwpf.usermodel.*, org.apache.poi.poifs.filesystem.POIFSFileSystem" %>
<%
String fileName = "D:testwebUB.doc";
//if (args.length > 0) { fileName = args[0];}
InputStream fis = new FileInputStream(fileName);
POIFSFileSystem fs = new POIFSFileSystem(fis);
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
String a6;
for (int i=0; i<range.numParagraphs(); i++) {
Paragraph par = range.getParagraph(i);
a6=par.text();
out.println(a6);
}
%>
ขึ้นไปด้านบน
