Skip to content

Commit

Permalink
feat: CsvExcelConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
wangmin9 committed Jan 8, 2024
1 parent d6ff754 commit c8dfae9
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
6 changes: 6 additions & 0 deletions jcommon/excel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.8</version>
</dependency>

</dependencies>

</project>
93 changes: 93 additions & 0 deletions jcommon/excel/src/main/java/run/mone/excel/CsvExcelConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package run.mone.excel;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author wmin
* @date 2024/1/8
*/
public class CsvExcelConverter {
//csv转excel
public static void convertCsvToExcel(String csvFilePath, String excelFilePath) throws IOException {
try (
InputStream csvInputStream = new FileInputStream(csvFilePath);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(csvInputStream));
Workbook workbook = new XSSFWorkbook();
FileOutputStream excelOutputStream = new FileOutputStream(excelFilePath)
) {
String line;
Sheet sheet = workbook.createSheet("Sheet1");
int rowNumber = 0;
while ((line = bufferedReader.readLine()) != null) {
String[] values = line.split(",");
Row row = sheet.createRow(rowNumber++);
for (int i = 0; i < values.length; i++) {
row.createCell(i).setCellValue(values[i]);
}
}
workbook.write(excelOutputStream);
}
}

//excel转csv
public static void convertExcelToCsv(String excelFilePath, String csvFilePath) throws IOException, InvalidFormatException {
try (Workbook workbook = WorkbookFactory.create(new File(excelFilePath));
CSVPrinter csvPrinter = new CSVPrinter(new FileWriter(csvFilePath), CSVFormat.DEFAULT)) {
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
List<String> csvValues = new ArrayList<>();
row.forEach(cell -> {
String text = new DataFormatter().formatCellValue(cell);
csvValues.add(text);
});
csvPrinter.printRecord(csvValues);
}
}
}

//给定一个csv文件,将所有所有列名a=x的行的列名b的值修改为y
public static void updateCsvColumnWhereAnotherColumnEquals(String csvFilePath, String updatedCsvFilePath, String targetColumnName, String conditionColumnName, String conditionValue, String newValue) throws IOException {
try (
Reader reader = Files.newBufferedReader(Paths.get(csvFilePath));
Writer writer = Files.newBufferedWriter(Paths.get(updatedCsvFilePath));
CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT.withFirstRecordAsHeader());
CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(parser.getHeaderMap().keySet().toArray(new String[0])))
) {
Map<String, Integer> headerMap = parser.getHeaderMap();
Integer targetColumnIndex = headerMap.get(targetColumnName);
Integer conditionColumnIndex = headerMap.get(conditionColumnName);

if (targetColumnIndex == null || conditionColumnIndex == null) {
throw new IllegalArgumentException("Column name not found in the CSV file");
}

// Iterate through records and update the target column where condition matches
for (CSVRecord record : parser) {
List<String> updatedRecord = new ArrayList<>();
for (String value : record) {
updatedRecord.add(value);
}

if (record.get(conditionColumnIndex).equals(conditionValue)) {
updatedRecord.set(targetColumnIndex, newValue);
}

printer.printRecord(updatedRecord);
}
}
}

}
40 changes: 40 additions & 0 deletions jcommon/excel/src/test/java/MyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import org.junit.Test;
import run.mone.excel.CsvExcelConverter;

import java.io.IOException;

/**
* @author wmin
* @date 2024/1/8
*/
public class MyTest {

@Test
public void testConvertCsvToExcel() {
try {
CsvExcelConverter.convertCsvToExcel("/Users/wmin/Downloads/miline_scaleOrder.csv","/Users/wmin/Downloads/miline_scaleOrder_0.xlsx");
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Test
public void testConvertExcelToCsv() {
try {
CsvExcelConverter.convertExcelToCsv("/Users/wmin/Downloads/miline_scaleOrder.xlsx","/Users/wmin/Downloads/miline_scaleOrder_0.csv");
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Test
public void testUpdateCsvColumnWhereAnotherColumnEquals() {
try {
CsvExcelConverter.updateCsvColumnWhereAnotherColumnEquals("/Users/wmin/Downloads/miline_scaleOrder.csv",
"/Users/wmin/Downloads/miline_scaleOrder1.csv",
"env", "id","60267","online");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit c8dfae9

Please sign in to comment.