資源簡介
java讀取doc、docx、slx、xlsx等word和excel文件,方法都集合在Read類中,返回一個HTML路徑,可以用在WEB開發上,功能強大

代碼片段和文件信息
import?java.io.*;
import?java.text.*;
import?java.util.ArrayList;
import?java.util.Date;
import?java.util.List;
import?java.util.zip.*;
import?javax.xml.parsers.*;
import?org.apache.poi.hwpf.HWPFDocument;
import?org.apache.poi.hwpf.usermodel.Range;
import?org.apache.poi.hwpf.usermodel.TableIterator;
import?org.w3c.dom.Document;
import?org.w3c.dom.Element;
import?org.w3c.dom.NodeList;
public?class?Excel?{
public?Range?range?=?null;
public?HWPFDocument?hwpf?=?null;
public?String?htmlPath;
public?String?picturePath;
public?List?pictures;
public?TableIterator?tableIterator;
public?int?presentPicture?=?0;
public?int?screenWidth;
public?FileOutputStream?output;
public?File?myFile;
StringBuffer?lsb?=?new?StringBuffer();
private?String?date?=?new?SimpleDateFormat(“yyyyMMddHHmmss“).format(new?Date());
//?新建txt文件
private?String?xlsx?=?“d:\\doc\\XLSX“?+?date?+?“.txt“;
//?xls源文件
public?static?void?main(String[]?args){
}
/**
??*?得到xlsx中的內容
??*/
public?void?readText(String?file)?throws?Exception{
String?text?=?null;
try?{
this.myFile?=?new?File(this.htmlPath);//new一個File路徑為html文件
this.output?=?new?FileOutputStream(this.myFile);//new一個流目標為html文件
String?head?=?“ta?charset=\“utf-8\“>“;//定義頭文件我在這里加了utf-8不然會出現亂碼
String?tableBegin?=?“yle=\“border-collapse:collapse\“?border=1?bordercolor=\“black\“>“;
String?tableEnd?=?“
“;
String?rowBegin?=?““;
String?rowEnd?=?“ “;
String?colBegin?=?““;
String?colEnd?=?“ “;
String?end?=?““;
this.output.write(head.getBytes());
this.output.write(tableBegin.getBytes());
?//???????String?str?=?““;
????????String?v?=?null;
????????boolean?flat?=?false;
????????List?ls?=?new?ArrayList();
//?解壓Book1.xlsx
ZipFile?xlsxFile?=?new?ZipFile(new?File(file));
DocumentBuilderFactory?dbf?=?DocumentBuilderFactory.newInstance();
//?先讀取sharedStrings.xml這個文件備用
ZipEntry?sharedStringxml?=?xlsxFile.getEntry(“xl/sharedStrings.xml“);
InputStream?sharedStringxmlIS?=?xlsxFile.getInputStream(sharedStringxml);
Document?sharedString?=?dbf.newDocumentBuilder().parse(sharedStringxmlIS);
NodeList?str?=?sharedString.getElementsByTagName(“t“);
String?sharedStrings[]?=?new?String[str.getLength()];
for?(int?n?=?0;?n? Element?element?=?(Element)?str.item(n);
?ls.add(element.getNodeValue());
//sharedStrings[n]?=?element.getTextContent();
}
//?找到解壓文件夾里的workbook.xml此文件中包含了這張工作表中有幾個sheet
ZipEntry?workbookxml?=?xlsxFile.getEntry(“xl/workbook.xml“);
InputStream?workbookxmlIS?=?xlsxFile.getInputStream(workbookxml);
Document?doc?=?dbf.newDocumentBuilder().parse(workbookxmlIS);
//?獲取一共有幾個sheet
NodeList?nl?=?doc.getElementsByTagName(“sheet“);
for?(int?i?=?0;?i? Element?el
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2013-01-28?08:41??Fileread\
?????文件????????1019??2013-01-25?09:56??Fileread\.classpath
?????文件?????????384??2013-01-24?14:44??Fileread\.project
?????目錄???????????0??2013-01-28?08:41??Fileread\.settings\
?????文件?????????598??2013-01-24?14:44??Fileread\.settings\org.eclipse.jdt.core.prefs
?????目錄???????????0??2013-01-28?08:41??Fileread\bin\
?????文件????????5544??2013-01-26?10:45??Fileread\bin\Excel.class
?????文件???????21983??2013-01-26?16:54??Fileread\bin\Read.class
?????文件????????6593??2013-01-26?11:02??Fileread\bin\word.class
?????目錄???????????0??2013-01-28?08:41??Fileread\src\
?????文件????????5060??2013-01-26?10:45??Fileread\src\Excel.java
?????文件???????23418??2013-01-26?16:54??Fileread\src\Read.java
?????文件???????15654??2013-01-26?11:02??Fileread\src\word.java
評論
共有 條評論