9
【代码】Java将CSV文件转换为Excel文件
source link: https://loli.fj.cn/2023/03/17/Java%E5%B0%86CSV%E6%96%87%E4%BB%B6%E8%BD%AC%E6%8D%A2%E4%B8%BAExcel%E6%96%87%E4%BB%B6/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
【代码】Java将CSV文件转换为Excel文件
2023-03-17
Java将CSV文件转换为Excel文件
package com;
import com.spire.xls.*;
import java.util.EnumSet;
public class Tools {
/**
* CSV文件转换为Excel文件
* @param dir 目录名:`/home/`、`C:/Users/
* @param filename 文件名:`filename`
*/
public static void convertCsvToExcel(String dir, String filename) {
//加载CSV文件
Workbook workbook = new Workbook();
workbook.loadFromFile(dir + filename + ".csv", ",", 1, 1);
//获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//访问工作表中使用的范围
CellRange usedRange = sheet.getAllocatedRange();
//当将范围内的数字保存为文本时,忽略错误
usedRange.setIgnoreErrorOptions(EnumSet.of(IgnoreErrorType.NumberAsText));
//自适应行高、列宽
usedRange.autoFitColumns();
usedRange.autoFitRows();
//保存文档
workbook.saveToFile(dir + filename + ".xlsx", ExcelVersion.Version2013);
}
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK