Java Spire.xls将excel转化为html等格式
直接上代码
Spire.xls文档地址: https://www.e-iceblue.cn/spirexlsforjava/spire-xls-for-java-program-guide-content.html
免费版: https://www.e-iceblue.cn/Introduce/Free-Spire-XLS-JAVA.html
转 html
package demo;import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;import javax.imageio.ImageIO;import com.spire.xls.ConverterSetting;
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;public class Demo {//Excel所在目录public static final String EXCELPATH = "E:\\files\\2021\\{0}";//Html输出目录public static final String HTMLPATH = "E:\\files\\2021html\\{0}.html";//Excel所在文件夹public static final String FILEPATH = "E:\\files\\2021"public static void main(String[] args) throws IOException {List<String> fileList = getFile(FILEPATH);for (String f : fileList) {System.out.println(MessageFormat.format(EXCELPATH, f));System.out.println(MessageFormat.format(HTMLPATH, f.substring(0, f.indexOf("."))));excel2Html(MessageFormat.format(EXCELPATH, f),MessageFormat.format(HTMLPATH, f.substring(0, f.indexOf("."))));}}public static void excel2Html(String excelPath, String htmlPath) throws IOException {Workbook wb = new Workbook();wb.loadFromFile(excelPath);//获取Excel的第一张表,要其他表就把0换成其它数字Worksheet sheet = wb.getWorksheets().get(0);//删除空白,否则输出的html可能会有大片空白,运行不了就把下面这两个循环删了for (int i = sheet.getLastRow(); i >= 1; i--){//判断行是否空白if (sheet.getRows()[i-1].isBlank()){//删除指定行sheet.deleteRow(i);}}//遍历所有列for (int j = sheet.getLastColumn(); j >= 1; j--){//判断列是否空白if (sheet.getColumns()[j-1].isBlank()){//删除指定列sheet.deleteColumn(j);}}sheet.saveToHtml(htmlPath);}//遍历需要转换目录下的所有excel表格的文件名public static List<String> getFile(String path) {File file = new File(path);File[] files = file.listFiles();List<String> fileList = new ArrayList<String>();for (File f : files) {fileList.add(f.getName());}return fileList;}
}
转pdf或其他
package demo;import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;import javax.imageio.ImageIO;import com.spire.xls.ConverterSetting;
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;public class Demo {//Excel所在目录public static final String EXCELPATH = "E:\\files\\2021\\{0}";//Html输出目录public static final String PDFPATH = "E:\\files\\2021pdf\\{0}.pdf";//Excel所在文件夹public static final String FILEPATH = "E:\\files\\2021"public static void main(String[] args) throws IOException {List<String> fileList = getFile(FILEPATH );for (String f : fileList) {System.out.println(MessageFormat.format(EXCELPATH, f));System.out.println(MessageFormat.format(PDFPATH, f.substring(0, f.indexOf("."))));excel2Pdf(MessageFormat.format(EXCELPATH, f),MessageFormat.format(PDFPATH, f.substring(0, f.indexOf("."))));}}public static void excel2Pdf(String excelPath, String pdfPath) throws IOException {Workbook wb = new Workbook();wb.loadFromFile(excelPath);//设置自适应,根据需求使用,就方法字面意思
// wb.getConverterSetting().setSheetFitToPage(true);
// wb.getConverterSetting().setSheetFitToWidth(true);//FileFormat下还有很多其它格式,可以自己试试wb.saveToFile(pdfPath, FileFormat.PDF);}//遍历需要转换目录下的所有excel表格的文件名public static List<String> getFile(String path) {File file = new File(path);File[] files = file.listFiles();List<String> fileList = new ArrayList<String>();for (File f : files) {fileList.add(f.getName());}return fileList;}
}