import java.awt.Image;
import java.io.BufferedWriter;
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.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
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.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
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.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.xml.*;
public class Read {
private String nameStr;
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();
OutputStreamWriter out1;
BufferedWriter bw;
String content;
public static void main(String[] args) {
Read r=new Read();
}
public Read(){
nameStr = "I:\\Test.doc";
read();
}
public void read(){
if(nameStr.endsWith(".doc")){
getRange();
makeFile();
this.readDOC();
System.out.println("htmlPath" + this.htmlPath);
}
if(this.nameStr.endsWith(".docx")){
try {
this.makeFile();
this.readDOCX();
writeHTML();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("htmlPath" + this.htmlPath);
}
if(this.nameStr.endsWith(".xls")){
try {
this.makeFile();
this.readXLS();
System.out.println("htmlPath" + this.htmlPath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(this.nameStr.endsWith(".xlsx")){
this.makeFile();
try {
content=readXLSX();
writeHTML();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("htmlPath: " + this.htmlPath);
}
}
/*读取word中的内容写到sdcard上的.html文件中*/
public void readDOC(){
try{
myFile = new File(htmlPath);
output = new FileOutputStream(myFile);
String head = "<html><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++){
Paragraph p2 = range.getParagraph(m);
temp++;
}
output.write(rowEnd.getBytes());
}
output.write(tableEnd.getBytes());
}
i = temp;
}
else{
output.write(tagBegin.getBytes());
writeParagraphContent(p);
output.write(tagEnd.getBytes());
}
}
String end = "</body></html>";
output.write(end.getBytes());
output.close();
}
catch(Exception e){
System.out.println("readAndWrite Exception");
}
}
public StringBuffer readXLS() throws Exception {
myFile = new File(htmlPath);
output = new FileOutputStream(myFile);
lsb.append("<html>");
lsb.append("<head>");
HSSFSheet sheet = null;
String excelFileName =nameStr;
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
excelFileName)); // 获整个Excel
lsb.append("<table width=\"100%\" style=\"border:1px solid #000;border-width:1px 0 0 1px;margin:2px 0 2px 0;border-collapse:collapse;\">");
for ( int sheetIndex=0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++) {
sheet = workbook.getSheetAt(sheetIndex);// 获所有的sheet
String sheetName = workbook.getSheetName(sheetIndex); // sheetName
if (workbook.getSheetAt(sheetIndex) != null) {
sheet = workbook.getSheetAt(sheetIndex);// 获得不为空的这个sheet
if (sheet != null) {
int firstRowNum = sheet.getFirstRowNum(); // 第一行
int lastRowNum = sheet.getLastRowNum(); // 最后一行
// 构造Table
for (int rowNum = firstRowNum; rowNum <= lastRowNum; rowNum++) {
if (sheet.getRow(rowNum) != null) {// 如果行不为空,
HSSFRow row = sheet.getRow(rowNum);
int height = (int) (row.getHeight() / 15.625); // 行的高度
lsb.append("<tr height=\""
+ height
+ "\" style=\"border:1px solid #000;border-width:0 1px 1px 0;margin:2px 0 2px 0;\">");
short firstCellNum = row.getFirstCellNum(); // 该行的第一个单元格
short lastCellNum = row.getLastCellNum(); // 该行的最后一个单元格
for (short cellNum = firstCellNum; cellNum <= lastCellNum; cellNum++) { // 循环该行的每一个单元格
HSSFCell cell = row.getCell(cellNum);
StringBuffer tdStyle = new StringBuffer(
"<td style=\"border:1px solid #000; border-width:0 1px 1px 0;margin:2px 0 2px 0; ");
if (cell != null) {
if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
continue;
} else {
HSSFCellStyle cellStyle = cell
.getCellStyle();
HSSFPalette palette = workbook
.getCustomPalette(); // 类HSSFPalette用于求颜色的国
- 1
- 2
- 3
前往页