Android實(shí)現(xiàn)excel/pdf/word/odt/圖片相互轉(zhuǎn)換
實(shí)踐過(guò)程
pdf轉(zhuǎn)excel
public static long pdfToExcel(String inFile, String outFile) throws Exception {
if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
return 0;
}
try {
long old = System.currentTimeMillis();
Document doc = new Document(inFile);
ExcelSaveOptions options = new ExcelSaveOptions();
options.setFormat(ExcelSaveOptions.ExcelFormat.XLSX);
doc.save(outFile, options);
Out.print(inFile, outFile, System.currentTimeMillis(), old);
return new File(outFile).length();
}catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
}
excel轉(zhuǎn)pdf
public static long excelToPdf(String inFile, String outFile) throws Exception {
if (!com.yrnet.transfer.business.transfer.file.License.getExcelLicense()) {
return 0;
}
try {
long old = System.currentTimeMillis();
File pdfFile = new File(outFile);
Workbook wb = new Workbook(inFile);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setOnePagePerSheet(true);
FileOutputStream fileOS = new FileOutputStream(pdfFile);
wb.save(fileOS, SaveFormat.PDF);
fileOS.close();
long now = System.currentTimeMillis();
Out.print(inFile, outFile, now, old);
return pdfFile.length();
}catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
}ppt轉(zhuǎn)pdf
public static long pptToPdf(String inFile, String outFile) throws Exception {
if (!com.yrnet.transfer.business.transfer.file.License.getPptLicense()) {
return 0;
}
try {
long old = System.currentTimeMillis();
File pdfFile = new File(outFile);
FileOutputStream os = new FileOutputStream(pdfFile);
Presentation pres = new Presentation(inFile);
pres.save(os, com.aspose.slides.SaveFormat.Pdf);
os.close();
long now = System.currentTimeMillis();
Out.print(inFile, outFile, now, old);
return pdfFile.length();
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
}pdf轉(zhuǎn)ppt
public static long pdfToPpt(String inFile, String outFile) {
if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
return 0;
}
long old = System.currentTimeMillis();
Document pdfDocument = new Document(inFile);
PptxSaveOptions pptxOptions = new PptxSaveOptions();
pptxOptions.setExtractOcrSublayerOnly(true);
pdfDocument.save(outFile, pptxOptions);
long now = System.currentTimeMillis();
Out.print(inFile, outFile, now, old);
return new File(outFile).length();
}
pdf轉(zhuǎn)word
public static long pdfToDoc(String inFile, String outFile) {
if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
return 0;
}
log.info("開(kāi)始轉(zhuǎn)換...");
long old = System.currentTimeMillis();
Document pdfDocument = new Document(inFile);
DocSaveOptions saveOptions = new DocSaveOptions();
/** 或者DocSaveOptions.DocFormat.DocX*/
saveOptions.setFormat(DocSaveOptions.DocFormat.Doc);
pdfDocument.save(outFile, saveOptions);
long now = System.currentTimeMillis();
Out.print(inFile, outFile, now, old);
log.info("轉(zhuǎn)換結(jié)束...");
return new File(outFile).length();
}word轉(zhuǎn)pdf
public static long wordToPdf(String inFile, String outFile) throws Exception {
if (!com.yrnet.transfer.business.transfer.file.License.getWordLicense()) {
return 0;
}
try {
long old = System.currentTimeMillis();
File file = new File(outFile);
FileOutputStream os = new FileOutputStream(file);
Document doc = new Document(inFile);
Document tmp = new Document();
tmp.removeAllChildren();
tmp.appendDocument(doc, ImportFormatMode.USE_DESTINATION_STYLES);
System.out.println("開(kāi)始解析word文檔" + inFile);
doc.save(os, SaveFormat.PDF);
long now = System.currentTimeMillis();
log.info("target file size:{}",file.length());
os.close();
Out.print(inFile, outFile, now, old);
return file.length();
} catch (Exception e) {
log.error(inFile + "轉(zhuǎn)換失敗,請(qǐng)重試",e);
throw new Exception(e.getMessage());
}
}excel轉(zhuǎn)圖片
public static long excelToPic(String inFile, String outFile) throws Exception {
if (!com.yrnet.transfer.business.transfer.file.License.getExcelLicense()) {
return 0;
}
try {
long old = System.currentTimeMillis();
Workbook wb = new Workbook(inFile);
Worksheet sheet = wb.getWorksheets().get(0);
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setImageFormat(ImageFormat.getPng());
imgOptions.setCellAutoFit(true);
imgOptions.setOnePagePerSheet(true);
SheetRender render = new SheetRender(sheet, imgOptions);
render.toImage(0, outFile);
long now = System.currentTimeMillis();
Out.print(inFile, outFile, now, old);
return new File(outFile).length();
}catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
}pdf轉(zhuǎn)圖片
public static long pdfToPng(String inFile, List<String> outFile) throws Exception {
long size = 0;
if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
return size;
}
try {
long old = System.currentTimeMillis();
Document pdfDocument = new Document(inFile);
Resolution resolution = new Resolution(960);
JpegDevice jpegDevice = new JpegDevice(resolution);
for (int index=1;index<=pdfDocument.getPages().size();index++) {
String path = inFile.substring(0,inFile.lastIndexOf(".")) + "_"+index+".png";
File file = new File(path);
size += file.length();
FileOutputStream fileOs = new FileOutputStream(file);
jpegDevice.process(pdfDocument.getPages().get_Item(index), fileOs);
outFile.add(path);
fileOs.close();
long now = System.currentTimeMillis();
Out.print(inFile, path, now, old);
}
return size;
}catch (Exception e){
log.error(e.getMessage(),e);
throw new Exception(e.getMessage());
}
}odt轉(zhuǎn)pdf
public static long pdfToPng(String inFile, List<String> outFile) throws Exception {
long size = 0;
if (!com.yrnet.transfer.business.transfer.file.License.getPdfLicense()) {
return size;
}
try {
long old = System.currentTimeMillis();
Document pdfDocument = new Document(inFile);
Resolution resolution = new Resolution(960);
JpegDevice jpegDevice = new JpegDevice(resolution);
for (int index=1;index<=pdfDocument.getPages().size();index++) {
String path = inFile.substring(0,inFile.lastIndexOf(".")) + "_"+index+".png";
File file = new File(path);
size += file.length();
FileOutputStream fileOs = new FileOutputStream(file);
jpegDevice.process(pdfDocument.getPages().get_Item(index), fileOs);
outFile.add(path);
fileOs.close();
long now = System.currentTimeMillis();
Out.print(inFile, path, now, old);
}
return size;
}catch (Exception e){
log.error(e.getMessage(),e);
throw new Exception(e.getMessage());
}
}
以上就是Android實(shí)現(xiàn)excel/pdf/word/odt/圖片相互轉(zhuǎn)換的詳細(xì)內(nèi)容,更多關(guān)于Android excel pdf word odt圖片轉(zhuǎn)換的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android使用自定義View繪制漸隱漸現(xiàn)動(dòng)畫(huà)
這篇文章主要介紹了Android使用自定義View繪制漸隱漸現(xiàn)動(dòng)畫(huà)效果的相關(guān)內(nèi)容,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
Android Studio導(dǎo)入Eclipse項(xiàng)目時(shí).so庫(kù)文件的解決方法
這篇文章主要介紹了Android Studio導(dǎo)入Eclipse項(xiàng)目時(shí)項(xiàng)目中".so"庫(kù)文件的解決方法,需要的朋友可以參考下2018-06-06
Android Webview添加網(wǎng)頁(yè)加載進(jìn)度條實(shí)例詳解
這篇文章主要介紹了Android Webview添加網(wǎng)頁(yè)加載進(jìn)度條實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android開(kāi)發(fā)中使用achartengine繪制各種圖表的方法
這篇文章主要介紹了Android開(kāi)發(fā)中使用achartengine繪制各種圖表的方法,結(jié)合具體實(shí)例形式分析了Android基于圖表生成類(lèi)庫(kù)achartengine進(jìn)行圖表繪制的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Android編程實(shí)現(xiàn)提取網(wǎng)址鏈接的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)提取網(wǎng)址鏈接的方法,涉及Android針對(duì)字符串的正則匹配操作相關(guān)技巧,需要的朋友可以參考下2016-10-10
Android編程基于距離傳感器控制手機(jī)屏幕熄滅的方法詳解
這篇文章主要介紹了Android編程基于距離傳感器控制手機(jī)屏幕熄滅的方法,結(jié)合具體實(shí)例形式分析了Android距離傳感器的控制屏幕熄滅的實(shí)現(xiàn)方法與相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
Android?中的?Timer?和?TimerTask詳解
Timer?是?Java?中用于創(chuàng)建定時(shí)任務(wù)的類(lèi),它位于?java.util?包中,可以使用Timer來(lái)安排一次性或定期執(zhí)行的任務(wù),這篇文章主要介紹了Android?的?Timer?和?TimerTask,需要的朋友可以參考下2024-05-05
activity 獲取rootView 設(shè)置backGroundColor的方法
下面小編就為大家?guī)?lái)一篇activity 獲取rootView 設(shè)置backGroundColor的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
詳解Android項(xiàng)目多服務(wù)端接口適配(超簡(jiǎn)單)
這篇文章主要介紹了Android項(xiàng)目多服務(wù)端接口適配(超簡(jiǎn)單),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08

