Java Excel转PDF 转图片 转Doc文档等格式操作
pom依赖
<!-- https://mvnrepository.com/artifact/e-iceblue/spire.xls.free -->
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls.free</artifactId>
<version>5.1.0</version>
</dependency>
jar包方式
https://mvnrepository.com/artifact/e-iceblue/spire.xls.free/5.1.0
将Excel转换为Java中的图像
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
/**
* <p>
* Description: TODO
* </p>
*
* @author songzixian
* @version v2.0.0
* @create 2022-05-31 23:51
* @see com.songzixian.demo
*/
public class Test {
public static void main(String[] args) {
//创建一个工作簿实例
Workbook workbook = new Workbook();
// //获取指定Excel文件
workbook.loadFromFile("D:\\home\\moneyDimension.xlsx");
//设置x轴、y轴dpi (设置该参数可以调图片清晰度)
workbook.getConverterSetting().setXDpi(300);
workbook.getConverterSetting().setYDpi(300);
// //获取指定工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//转成图片并且存储
sheet.saveToImage("D:\\home\\image2.png");
}
}
将Excel转换为PDF
将整个Excel工作簿转换为PDF。
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
/**
* <p>
* Description: TODO
* </p>
*
* @author songzixian
* @version v2.0.0
* @create 2022-05-31 23:51
* @see com.songzixian.demo
*/
public class Test {
public static void main(String[] args) {
//创建一个工作簿实例
Workbook workbook = new Workbook();
// //获取指定Excel文件
workbook.loadFromFile("D:\\home\\moneyDimension.xlsx");
// //获取指定工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//转成图片并且存储
workbook.saveToFile("D:\\home\\szxblog.pdf", FileFormat.PDF);
System.out.println("转换成功");
}
}
将单个Excel工作表转换为PDF。
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
/**
* <p>
* Description: TODO
* </p>
*
* @author songzixian
* @version v2.0.0
* @create 2022-05-31 23:51
* @see com.songzixian.demo
*/
public class Test {
public static void main(String[] args) {
//创建一个工作簿实例
Workbook workbook = new Workbook();
// //获取指定Excel文件
workbook.loadFromFile("D:\\home\\moneyDimension.xlsx");
//G//获取指定工作表
Worksheet worksheet = workbook.getWorksheets().get(0);
worksheet.saveToPdf("D:\\home\\szxblog2.pdf");
System.out.println("转换成功");
}
}
将Excel转换为TIFF
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class ConvertExcelToTIFF {
public static void main(String[] args) {
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load the Excel file
workbook.loadFromFile("Input.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
//Save the first worksheet to TIFF
sheet.saveToTiff("SheetToTiff.tiff");
//Save a particular cell range in the first worksheet to TIFF
//sheet.saveToTiff("CellRangeToTiff.tiff",1,1,5,2);
}
}
将Excel转换为HTML
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
/**
* <p>
* Description: TODO
* </p>
*
* @author songzixian
* @version v2.0.0
* @create 2022-05-31 23:51
* @see com.songzixian.demo
*/
public class Test {
public static void main(String[] args) {
//创建一个工作簿实例
Workbook workbook = new Workbook();
// //获取指定Excel文件
workbook.loadFromFile("D:\\home\\moneyDimension.xlsx");
// //获取指定工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//支持多种格式
workbook.saveToFile("D:\\home\\szxblog.html", FileFormat.HTML);
System.out.println("转换成功");
}
}