package gwn.develop.widget;
import gwn.develop.widget.R;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFPictureData;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFShape;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;
import android.webkit.WebView;
/**
* 查看word,excel附件的界面
* @author chenhao
* 2013-5-15 上午10:08:12
*/
/**
* @author chenhao
* 2013-5-15 上午10:38:41
*/
public class FileReadActivity extends Activity {
/**
* 显示数据的界面
*/
private WebView viewWV;
public static final String FILEPATH = "filePath";
/**
* 文件路径
*/
private String filePath;
/**
* 文档范围
*/
private Range range = null;
/**
* 包装文档后的对象
*/
private HWPFDocument hwpf = null;
/**
* 图片集合
*/
private List<Picture> pictures;
/**
* 表格迭代器
*/
private TableIterator tableIterator;
/**
* html保存的路径
*/
private String htmlPath;
/**
* html文件
*/
private File myFile;
/**
* 写入html的全局文件输出流
*/
private FileOutputStream output;
/**
* 文档中当前图片的位置
*/
private int presentPicture = 0;
/**
* 图片路径
*/
private String picturePath;
/**
* 拼接html语言的字符串
*/
private StringBuffer lsb = new StringBuffer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.common_attachment_view);
viewWV = (WebView) this.findViewById(R.id.common_attr_view_wb);
viewWV.getSettings().setBuiltInZoomControls(true);
viewWV.getSettings().setUseWideViewPort(true);
viewWV.getSettings().setSupportZoom(true);
initData();
String returnPath = read();
viewWV.loadUrl(returnPath);
}
/**
* 初始化数据,获取文件路径
* 2013-5-15 上午10:35:51 chenhao添加此方法
*/
private void initData() {
Intent intent = getIntent();
filePath = intent.getStringExtra(FILEPATH);
}
/**
* 根据不同的文件实现不同读取
* 2013-5-15 上午10:40:16 chenhao添加此方法
* @param filePath
* @return
*/
private String read(){
String returnPath = "";
if(this.filePath.endsWith(".doc")){
this.getRange();
this.makeFile();
this.readDOC();
}
if(this.filePath.endsWith(".docx")){
this.makeFile();
this.readDOCX();
}
if(this.filePath.endsWith(".xls")){
try {
this.makeFile();
this.readXLS();
} catch (Exception e) {
e.printStackTrace();
}
}
if(this.filePath.endsWith(".xlsx")){
this.makeFile();
this.readXLSX();
}
returnPath = "file:///" + this.htmlPath;
return returnPath;
}
/**
* 得到文档的范围并初始化文档中的相关元素
* 2013-5-15 上午10:53:09 chenhao添加此方法
*/
private void getRange() {
FileInputStream in = null;
POIFSFileSystem pfs = null;
try {
in = new FileInputStream(filePath);
pfs = new POIFSFileSystem(in);
hwpf = new HWPFDocument(pfs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
range = hwpf.getRange();
hwpf.getPicturesTable();
pictures = hwpf.getPicturesTable().getAllPictures();
tableIterator = new TableIterator(range);
}
/**
* 创建html文件
* 2013-5-15 上午11:03:43 chenhao添加此方法
*/
private void makeFile() {
String sdStateString = android.os.Environment.getExternalStorageState();// 获取外部存储状态
if (sdStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {// 确认sd卡存在
try {
File sdFile = android.os.Environment
.getExternalStorageDirectory();// 获取扩展设备的文件目录
String path = sdFile.getAbsolutePath() + File.separator
+ "chenhao/attrs";// 得到sd卡(扩展设备)的绝对路径+"/"
File dirFile = new File(path);// 获取文件夹地址
if (!dirFile.exists()) {// 如果不存在
dirFile.mkdir();// 创建目录
}
File myFile = new File(path + File.separator + "my.html");// 获取my.html的地址
if (!myFile.exists()) {// 如果不存在
myFile.getParentFile().mkdirs();// 创建文件
}
this.htmlPath = myFile.getAbsolutePath();// 返回路径
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 读取word中的内容写到sdcard上的.html文件中
*/
public void readDOC() {
try {
myFile = new File(htmlPath);
output = new FileOutputStream(myFile);
String head = "<html><meta charset=\"utf-8\"><body>";
String tagBegin = "<p>";
String tagEnd = "</p>";
output.write(head.getBytes());
int numParagraphs = range.numParagraphs();// 得到页面所有的段落数
for (int i = 0; i < numParagraphs; i++) { // 遍历段落数
Paragraph p = range.getParagraph(i); // 得到文档中的每一个段落
if (p.isInTable()) {
int temp = i;
if (tableIterator.hasNext()) {
String tableBegin = "<table style=\"border-collapse:collapse\" border=1 bordercolor=\"black\">";
String tableEnd = "</table>";
String rowBegin = "<tr>";
String rowEnd = "</tr>";
String colBegin = "<td>";
String colEnd = "</td>";
Table table = tableIterator.next();
output.write(tableBegin.getBytes());
int rows = table.numRows();
for (int r = 0; r < rows; r++) {
output.write(rowBegin.getBytes());
TableRow row = table.getRow(r);
int cols = row.numCells();
int rowNumParagraphs = row.numParagraphs();
int colsNumParagraphs = 0;
for (int c = 0; c < cols; c++) {
output.write(colBegin.getBytes());
TableCell cell = row.getCell(c);
int max = temp + cell.numParagraphs();
colsNumParagraphs = colsNumParagraphs
+ cell.numParagraphs();
for (int cp = temp; cp < max; cp++) {
Paragraph p1 = range.getParagraph(cp);
output.write(tagBegin.getBytes());
writeParagraphContent(p1);
output.write(tagEnd.getBytes());
temp++;
}
output.write(colEnd.getBytes());
}
int max1 = temp + rowNumParagraphs;
for (int m = temp + colsNumParagraphs; m < max1; m++) {
temp++;
}
output.write(rowEnd.getBytes());
}
output.write(tableEnd.getBytes());
}
i = temp;
}
else
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
android应用源码tree目录和读取word文档整合-IT计算机-毕业设计.zip (55个子文件)
android应用源码tree目录和读取word文档整合
TreeListView
bin
classes.dex 41KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
background.png 135KB
ic_launcher.png 2KB
node_unexpand.png 990B
node_expand.png 1KB
TreeListView.apk 167KB
jarlist.cache 120B
dexedLibs
annotations-88b029774f029562adb525282f9d2acc.jar 943B
classes
gwn
develop
widget
BuildConfig.class 345B
R$layout.class 440B
FileReadActivity.class 26KB
TreeListAdapter$ViewHolder.class 614B
R$id.class 430B
R$drawable.class 513B
TreeNode.class 2KB
DataProvider.class 2KB
PicInfo.class 1KB
TreeListViewActivity.class 2KB
R$attr.class 340B
R.class 531B
TreeListAdapter.class 3KB
R$string.class 427B
TreeListAdapter$1.class 4KB
2012-04-27TreeListView.apk 154KB
resources.ap_ 149KB
AndroidManifest.xml 886B
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
values
strings.xml 200B
drawable-mdpi
background.png 147KB
ic_launcher.png 3KB
node_unexpand.png 990B
node_expand.png 1KB
layout
common_attachment_view.xml 382B
main.xml 582B
gen
gwn
develop
widget
BuildConfig.java 160B
R.java 1KB
proguard.cfg 1KB
src
gwn
develop
widget
TreeListViewActivity.java 869B
FileReadActivity.java 32KB
TreeListAdapter.java 4KB
PicInfo.java 1000B
DataProvider.java 1KB
TreeNode.java 2KB
.project 848B
.classpath 800B
project.properties 361B
AndroidManifest.xml 886B
libs
poi-ooxml-3.8-20120326.jar 911KB
android-support-v4.jar 384KB
poi-3.9-20121203.jar 1.78MB
poi-scratchpad-3.8-20120326.jar 1.13MB
xmlbeans-2.3.0_poi.jar 2.54MB
共 55 条
- 1
资源评论
芝麻粒儿
- 粉丝: 6w+
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- keil2 + proteus + 8051.exe
- 1961ee27df03bd4595d28e24b00dde4e_744c805f7e4fb4d40fa3f695bfbab035_8(1).c
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
- windows注册表编辑工具
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
- 校园通行码预约管理系统20240522075502
- 车类型数据集6250张VOC+YOLO格式.zip
- The PyTorch implementation of STGCN.STGCN-main.zip
- 092300108.cpp
- 车类型数据集6000张VOC+YOLO格式.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功