首先grable文件导入jar包,
implementation 'org.apache.poi:poi:3.17'
// Excel XLS
implementation 'org.apache.poi:poi-ooxml:3.17'
// Excel XLSX DOCX
implementation 'org.apache.poi:poi-scratchpad:3.17'
// Word DOC
implementation 'org.apache.poi:poi-ooxml-schemas:3.17'
implementation files('libs/xdocreport-2.0.1.jar')
implementation files('libs/rt.jar')
//项目要使用iText
implementation 'com.itextpdf:itextpdf:5.5.13.1'
implementation 'com.itextpdf.tool:xmlworker:5.5.11'
implementation 'org.apache.xmlbeans:xmlbeans:3.1.0'
implementation 'org.apache.commons:commons-collections4:4.4'
//输出中文,还要引入下面itext-asian.jar包
implementation 'com.itextpdf:itext-asian:5.2.0'
implementation 'org.jsoup:jsoup:1.11.3'
运行项目中可能会有重复包或文件,在grable的android中添加:
packagingOptions {
exclude 'com/itextpdf/text/pdf/fonts/cmap_info.txt'
exclude 'MATE-INF/LICENSE'
}
解决重复引入包的问题。
核心代码:
import android.util.Log;
import org.apache.poi.hssf.converter.ExcelToHtmlConverter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.w3c.dom.Document;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
public class ExcelToHtml {
public static String readExcelToHtml(String xlsPath) {
try {
Workbook wb = readExcel(xlsPath);
return excel07ToHtml(wb);
// if (wb instanceof XSSFWorkbook) {
// XSSFWorkbook XWb = (XSSFWorkbook) wb;
// return excel07ToHtml(XWb);
// } else if (wb instanceof HSSFWorkbook) {
// HSSFWorkbook HWb = (HSSFWorkbook) wb;
// return excel03ToHtml(HWb);
// } else {
// return new String("Temporary does not support.".getBytes(), StandardCharsets.UTF_8);
// }
} catch (Exception e) {
e.printStackTrace();
Log.e("ExcelToHtml", "e:" + e.getMessage());
}
return null;
}
private static Workbook readExcel(String fileName) {
Workbook wb = null;
if (fileName == null) {
return null;
}
String extString = fileName.substring(fileName.lastIndexOf("."));
InputStream is = null;
try {
is = new FileInputStream(fileName);
if (".xls".equals(extString)) {
return wb = new HSSFWorkbook(is);
} else if (".xlsx".equals(extString)) {
return wb = new XSSFWorkbook(is);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wb;
}
/**
* excel03转html
* filename:要读取的文件所在文件夹
* filepath:文件名
* htmlname:生成html名称
* path:html存放路径
*/
public static String excel03ToHtml(HSSFWorkbook excelBook) throws ParserConfigurationException, TransformerException, IOException {
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
excelToHtmlConverter.processWorkbook(excelBook);//excel转html
Document htmlDocument = excelToHtmlConverter.getDocument();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();//字节数组输出流
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(outStream);
/** 将document中的内容写入文件中,创建html页面 */
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
outStream.close();
return outStream.toString("UTF-8");
}
private static Map map[];
/**
* excel07转html
* filename:要读取的文件所在文件夹
* filepath:文件名
* htmlname:生成html名称
* path:html存放路径
*/
public static String excel07ToHtml(Workbook workbook) {
ByteArrayOutputStream baos = null;
StringBuilder html = new StringBuilder();
try {
for (int numSheet = 0; numSheet < workbook.getNumberOfSheets(); numSheet++) {
Sheet sheet = workbook.getSheetAt(numSheet);
if (sheet == null) {
continue;
}
html.append("=======================").append(sheet.getSheetName()).append("=========================
");
int firstRowIndex = sheet.getFirstRowNum();
int lastRowIndex = sheet.getLastRowNum();
// html.append("
html.append("
map = getRowSpanColSpanMap(sheet);
//行
for (int rowIndex = firstRowIndex; rowIndex <= lastRowIndex; rowIndex++) {
Row currentRow = sheet.getRow(rowIndex);
if (null == currentRow) {
html.append("
");continue;
} else if (currentRow.getZeroHeight()) {
continue;
}
html.append("
");int firstColumnIndex = currentRow.getFirstCellNum();
int last