androidoffice转pdf插件,AndroidwordExcel⽂档转PDF ⾸先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 'l:xmlworker:5.5.11'
implementation 'lbeans:xmlbeans:3.1.0'
implementation 'org.apachemons: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/'
exclude 'MATE-INF/LICENSE'
}
解决重复引⼊包的问题。
核⼼代码:
import android.util.Log;
import org.apache.verter.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;
excel最强教科书完全版pdf
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
l.parsers.DocumentBuilderFactory; l.parsers.ParserConfigurationException; l.transform.OutputKeys;
l.transform.Transformer;
l.transform.TransformerException; l.transform.TransformerFactory;
l.transform.dom.DOMSource;
l.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
wInstance().newDocumentBuilder().newDocument());
excelToHtmlConverter.processWorkbook(excelBook);//excel转html
Document htmlDocument = Document();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();//字节数组输出流
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(outStream);
/** 将document中的内容写⼊⽂件中,创建html页⾯ */
TransformerFactory tf = wInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
outStream.close();
String("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 < NumberOfSheets(); numSheet++) {
Sheet sheet = SheetAt(numSheet);
if (sheet == null) {
continue;
}
html.append("=======================").SheetName()).append("========================= ");
int firstRowIndex = FirstRowNum();
int lastRowIndex = LastRowNum();
// html.append("
html.append("
map = getRowSpanColSpanMap(sheet);
//⾏
for (int rowIndex = firstRowIndex; rowIndex <= lastRowIndex; rowIndex++) {
Row currentRow = Row(rowIndex);
if (null == currentRow) {
html.append("
");
continue;
} else if (ZeroHeight()) {
continue;
}
html.append("
");
int firstColumnIndex = FirstCellNum();
int lastColumnIndex = LastCellNum();
//列
for (int columnIndex = firstColumnIndex; columnIndex <= lastColumnIndex; columnIndex++) {
Cell currentCell = Cell(columnIndex);
if (currentCell == null) {
continue;
}
String currentCellValue = getCellValue(currentCell);
if (map[0].containsKey(rowIndex + "," + columnIndex)) {
String pointString = (String) map[0].get(rowIndex + "," + columnIndex);
int bottomeRow = Integer.valueOf(pointString.split(",")[0]);
int bottomeCol = Integer.valueOf(pointString.split(",")[1]);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。