资源简介
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 < str.getLength(); 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 < nl.getLength(); 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
相关资源
- 微博系统(Java源码,servlet+jsp),适
- java串口通信全套完整代码-导入eclip
- jsonarray所必需的6个jar包.rar
- 三角网构TIN生成算法,Java语言实现
- java代码编写将excel数据导入到mysql数据
- Java写的cmm词法分析器源代码及javacc学
- JAVA JSP公司财务管理系统 源代码 论文
- JSP+MYSQL旅行社管理信息系统
- 推荐算法的JAVA实现
- 基于Java的酒店管理系统源码(毕业设
- java-图片识别 图片比较
- android毕业设计
- java23种设计模式+23个实例demo
- java Socket发送/接受报文
- JAVA828436
- java界面美化 提供多套皮肤直接使用
- 在线聊天系统(java代码)
- 基于Java的图书管理系统807185
- java中实现将页面数据导入Excel中
- java 企业销售管理系统
- java做的聊天系统(包括正规课程设计
- Java编写的qq聊天室
- 商店商品管理系统 JAVA写的 有界面
- JAVA开发聊天室程序
- 在linux系统下用java执行系统命令实例
- java期末考试试题两套(答案) 选择(
- JAVA3D编程示例(建模、交互)
- Java 文件加密传输
- java做的房产管理系统
- 基于jsp的bbs论坛 非常详细
评论
共有 条评论