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 's: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.urmodel.HSSFWorkbook;
import org.apache.poi.ss.urmodel.Cell;
import org.apache.poi.ss.urmodel.CellStyle;
import org.apache.poi.ss.urmodel.CellType;
import org.apache.poi.ss.urmodel.HorizontalAlignment;
import org.apache.poi.ss.urmodel.Row;
import org.apache.poi.ss.urmodel.Sheet;
import org.apache.poi.ss.urmodel.VerticalAlignment; import org.apache.poi.ss.urmodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.urmodel.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.chart.StandardCharts;
import java.util.HashMap;
import java.util.Map;
l.parrs.DocumentBuilderFactory; l.parrs.ParrConfigurationException; 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);
// } el if (wb instanceof HSSFWorkbook) {
// HSSFWorkbook HWb = (HSSFWorkbook) wb;
// return excel03ToHtml(HWb);
// } el {
// return new String("Temporary does not support.".getBytes(), StandardCharts.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)) {
deltereturn wb = new HSSFWorkbook(is);
} el 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 ParrConfigurationException, 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 rializer = tf.newTransformer();
rializer.tOutputProperty(OutputKeys.ENCODING, "utf-8");
rializer.tOutputProperty(OutputKeys.INDENT, "yes");
rializer.tOutputProperty(OutputKeys.METHOD, "html");
outStream.clo();
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;
} el 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]);