Commit 959d8e92 authored by hongguangwu's avatar hongguangwu

1.7.22-税友excel转图片换插件

parent def24911
...@@ -117,6 +117,11 @@ ...@@ -117,6 +117,11 @@
<artifactId>aspose-words</artifactId> <artifactId>aspose-words</artifactId>
<version>23.1</version> <version>23.1</version>
</dependency> </dependency>
<dependency>
<groupId>com.luhuiguo</groupId>
<artifactId>aspose-cells</artifactId>
<version>23.1</version>
</dependency>
</dependencies> </dependencies>
......
...@@ -92,7 +92,7 @@ public class TSocialFriendController { ...@@ -92,7 +92,7 @@ public class TSocialFriendController {
@SysLog("定时任务清理临时文件") @SysLog("定时任务清理临时文件")
@Inner @Inner
public void doClearFriend() { public void doClearFriend() {
tSocialFriendPushService.doClearFriend(); //tSocialFriendPushService.doClearFriend();
} }
@Operation(description = "2定时任务获取税友") @Operation(description = "2定时任务获取税友")
......
package com.yifu.cloud.plus.v1.yifu.social.util; package com.yifu.cloud.plus.v1.yifu.social.util;
import com.aspose.cells.ImageOrPrintOptions;
import com.aspose.cells.ImageType;
import com.aspose.cells.SheetRender;
import com.spire.xls.Workbook; import com.spire.xls.Workbook;
import com.spire.xls.Worksheet; import com.spire.xls.Worksheet;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants; import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
...@@ -18,7 +21,6 @@ import javax.imageio.ImageIO; ...@@ -18,7 +21,6 @@ import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
...@@ -35,6 +37,7 @@ import java.util.Base64; ...@@ -35,6 +37,7 @@ import java.util.Base64;
@Component @Component
public class ExcelToImage { public class ExcelToImage {
private final static String toImag ="\\ToImg.png"; private final static String toImag ="\\ToImg.png";
private static final String TO_IMAGE_TEMP ="\\ToImgTemp.png"; private static final String TO_IMAGE_TEMP ="\\ToImgTemp.png";
...@@ -56,14 +59,15 @@ public class ExcelToImage { ...@@ -56,14 +59,15 @@ public class ExcelToImage {
File tempFile = null; File tempFile = null;
File compressedTempFile = null; File compressedTempFile = null;
//加载Excel工作表 //加载Excel工作表
Workbook wb = new Workbook(); //Workbook wb = new Workbook();
try { try {
/*wb.loadFromHtml(inputStream);
wb.loadFromHtml(inputStream);
//获取工作表 //获取工作表
Worksheet sheet = wb.getWorksheets().get(0); Worksheet sheet = wb.getWorksheets().get(0);
sheet.saveToImage(toImagePath);*/
String toImagePath = projectRoot + toImag; String toImagePath = projectRoot + toImag;
sheet.saveToImage(toImagePath); createWorksheetImage(inputStream, toImagePath);
tempFile = new File(toImagePath); tempFile = new File(toImagePath);
// 上传税友文件服务器,返回文件路径 // 上传税友文件服务器,返回文件路径
...@@ -89,9 +93,9 @@ public class ExcelToImage { ...@@ -89,9 +93,9 @@ public class ExcelToImage {
e.printStackTrace(); e.printStackTrace();
return R.failed(CommonConstants.RESULT_DATA_FAIL+toImagConvertError+e.getMessage()); return R.failed(CommonConstants.RESULT_DATA_FAIL+toImagConvertError+e.getMessage());
} finally { } finally {
if (wb != null) { /*if (wb != null) {
wb.dispose(); // 重要:释放资源并删除临时文件 wb.dispose(); // 重要:释放资源并删除临时文件
} }*/
if (tempFile != null && tempFile.exists()){ if (tempFile != null && tempFile.exists()){
tempFile.delete(); tempFile.delete();
} }
...@@ -262,4 +266,35 @@ public class ExcelToImage { ...@@ -262,4 +266,35 @@ public class ExcelToImage {
sheet.saveToFile("ToPCL.pcl", String.valueOf(FileFormat.PCL));*/ sheet.saveToFile("ToPCL.pcl", String.valueOf(FileFormat.PCL));*/
} }
/*public void excelToImageMain(String toImagePath) {
try {
createWorksheetImage(toImagePath);
} catch (Exception e) {
e.printStackTrace();
}
}*/
private static void createWorksheetImage(InputStream inputStream, String toImagePath) {
com.aspose.cells.Workbook workbook = null;
try {
workbook = new com.aspose.cells.Workbook(inputStream);
// 获取第一个工作表
com.aspose.cells.Worksheet worksheet = workbook.getWorksheets().get(0);
// 设置为适应内容大小
worksheet.getPageSetup().setZoom(100);
// 转换为图片
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.setImageType(ImageType.PNG);
options.setOnePagePerSheet(true);
SheetRender render = new SheetRender(worksheet, options);
render.toImage(0, toImagePath);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (workbook != null) {
workbook.dispose();
}
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment