diff --git a/README.md b/README.md index 921bade..cc9b92a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,11 @@ # csvconvert -简单的csv转xls工具 \ No newline at end of file +简单的csv转xls工具 + +## 使用说明 + +打包后运行命令行 + +```cmd +java -jar csvconvert.jar --f=csv文件.csv --c=csv文件编码格式 +``` diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8ebb3c9 --- /dev/null +++ b/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + io.lroyia + csvconvert + 1.0-SNAPSHOT + + + 8 + 8 + + + + + + org.apache.commons + commons-csv + 1.8 + + + + org.apache.poi + poi + 4.1.2 + + + + + \ No newline at end of file diff --git a/src/main/java/io/lroyia/ApplicationRun.java b/src/main/java/io/lroyia/ApplicationRun.java new file mode 100644 index 0000000..b0edd83 --- /dev/null +++ b/src/main/java/io/lroyia/ApplicationRun.java @@ -0,0 +1,86 @@ +package io.lroyia; + +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; +import org.apache.commons.csv.CSVRecord; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; +import java.util.List; + +/** + * 主启动类 + * @author lroyia + * @since 2021/3/22 14:46 + **/ +public class ApplicationRun { + + public static void main(String[] args) throws IOException { + String chartSet = "utf8"; + String fileUri = null; + for (String arg : args) { + if(arg.startsWith("--")) { + String[] argSplit = arg.split("="); + if(argSplit[0].equals("--f")){ + fileUri = argSplit[1]; + }else if(argSplit[0].equals("--c")){ + chartSet = argSplit[1]; + } + } + } + if(fileUri == null){ + System.out.println("please select a convert file."); + return; + } + File file = new File(fileUri); + String fileName = file.getName(); + String newFileName = fileName.replace(".csv", ".xls"); + try(CSVParser csv = CSVParser.parse(file, Charset.forName(chartSet), CSVFormat.DEFAULT); + OutputStream os = new FileOutputStream(newFileName)){ + List headerNames = csv.getHeaderNames(); + System.out.println(headerNames); + HSSFWorkbook workbook = new HSSFWorkbook(); + Sheet sheet = workbook.createSheet(); + Row header = sheet.createRow(0); + int colLength = headerNames.size(); + System.out.println(colLength); + List records = csv.getRecords(); + System.out.println(records.size()); + for (int i = 0; i < colLength; i++) { + createCell(header, i, headerNames.get(i)); + } + long lastCol = csv.getRecordNumber(); + for(int i = 0; i < lastCol; i++){ + Row curRow = sheet.createRow(i+1); + CSVRecord curRecord = records.get(i); + for(int j = 0; j < curRecord.size(); j++){ + createCell(curRow, j, curRecord.get(j)); + } + } + workbook.write(os); + } + + } + + /** + * 创建单元格并设置值 + * @param row 行 + * @param colIndex 列序号 + * @param value 值 + * @return 创建结果 + * @author lroyia + * @since 2021年3月22日 16:06:57 + */ + public static Cell createCell(Row row, int colIndex, String value){ + Cell cell = row.createCell(colIndex); + cell.setCellValue(value); + return cell; + } +}