Tôi cần chuyển đổi csv thành xls/xlsx trong dự án của mình? Làm thế nào tôi có thể làm điều đó? Bất cứ ai có thể gửi cho tôi một số ví dụ? Tôi muốn làm điều đó với Apache poi. Tôi cũng cần tạo một ô từ phía java.Chuyển csv sang xls/xlsx bằng Apache poi?
5
A
Trả lời
9
Bạn có thể thử phương pháp sau để tạo tệp xlsx bằng apache-poi.
public static void csvToXLSX() {
try {
String csvFileAddress = "test.csv"; //csv file address
String xlsxFileAddress = "test.xlsx"; //xlsx file address
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet sheet = workBook.createSheet("sheet1");
String currentLine=null;
int RowNum=0;
BufferedReader br = new BufferedReader(new FileReader(csvFileAddress));
while ((currentLine = br.readLine()) != null) {
String str[] = currentLine.split(",");
RowNum++;
XSSFRow currentRow=sheet.createRow(RowNum);
for(int i=0;i<str.length;i++){
currentRow.createCell(i).setCellValue(str[i]);
}
}
FileOutputStream fileOutputStream = new FileOutputStream(xlsxFileAddress);
workBook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("Done");
} catch (Exception ex) {
System.out.println(ex.getMessage()+"Exception in try");
}
}
3
Chúng ta có thể sử dụng SXSSF Jar trong đó chúng ta có thể phân tích một tập tin dài như sau:
public static void main(String[] args) {
try {
// String fName = args[ 0 ];
String csvFileAddress = "C:\\Users\\psingh\\Desktop\\test\\New folder\\GenericDealerReport - version6.txt"; //csv file address
String xlsxFileAddress = "C:\\Users\\psingh\\Desktop\\trial\\test3.xlsx"; //xlsx file address
SXSSFWorkbook workBook = new SXSSFWorkbook(1000);
org.apache.poi.ss.usermodel.Sheet sheet = workBook.createSheet("sheet1");
String currentLine = null;
int RowNum = -1;
BufferedReader br = new BufferedReader(new FileReader(csvFileAddress));
while ((currentLine = br.readLine()) != null) {
String str[] = currentLine.split("\\|");
RowNum++;
Row currentRow = sheet.createRow(RowNum);
for (int i = 0; i < str.length; i++) {
currentRow.createCell(i)
.setCellValue(str[ i ]);
}
}
DateFormat df = new SimpleDateFormat("yyyy-mm-dd-HHmmss");
Date today = Calendar.getInstance()
.getTime();
String reportDate = df.format(today);
FileOutputStream fileOutputStream = new FileOutputStream(xlsxFileAddress);
workBook.write(fileOutputStream);
fileOutputStream.close();
//System.out.println("Done");
}
catch (Exception ex) {
System.out.println(ex.getMessage() + "Exception in try");
}
}
2
Tôi đã tìm thấy SXSSFWorkbook
thực sự nhanh hơn sau đó XSSFWorkbook
. Đây là mã chỉnh sửa:
try {
String csvFileInput = "inputFile.csv";
String xlsxFileOutput ="outputFile.xls";
LOGGER.error(csvFileInput);
LOGGER.error(xlsxFileOutput);
SXSSFWorkbook workBook = new SXSSFWorkbook();
Sheet sheet = workBook.createSheet(transformBean.getOutputFileName());
String currentLine = null;
int RowNum = 0;
BufferedReader br = new BufferedReader(new FileReader(csvFileInput));
while ((currentLine = br.readLine()) != null) {
String str[] = currentLine.split(",");
RowNum++;
Row currentRow = sheet.createRow(RowNum);
for (int i = 0; i < str.length; i++) {
currentRow.createCell(i).setCellValue(str[i]);
}
}
FileOutputStream fileOutputStream = new FileOutputStream(xlsxFileOutput);
workBook.write(fileOutputStream);
fileOutputStream.close();
System.out.println("Done");
} catch (Exception ex) {
System.out.println(ex.getMessage() + "Found Exception");
}
0
if(new File(newFileName).isFile()) return;
@SuppressWarnings("resource")
HSSFWorkbook wb = new HSSFWorkbook();
Row xlsRow;
Cell xlsCell;
HSSFSheet sheet = wb.createSheet("sheet1");
int rowIndex = 0;
for(CSVRecord record : CSVFormat.EXCEL.parse(new FileReader(fileName))) {
xlsRow = sheet.createRow(rowIndex);
for(int i = 0; i < record.size(); i ++){
xlsCell = xlsRow.createCell(i);
xlsCell.setCellValue(record.get(i));
}
rowIndex ++;
}
FileOutputStream out = new FileOutputStream(newFileName);
wb.write(out);
out.close();
0
Hãy thử một trong này nếu bạn có inputstream public static XSSFWorkbook csvToXLSX (InputStream InputStream) throws IOException { XSSFWorkbook Workbook = new XSSFWorkbook();
try(BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
Sheet sheet = workBook.createSheet("sheet1");
String currentLine=null;
int rowNum=0;
while ((currentLine = br.readLine()) != null) {
String[] str = currentLine.split(",");
rowNum++;
Row currentRow=sheet.createRow(rowNum);
for(int i=0;i<str.length;i++){
currentRow.createCell(i).setCellValue(str[i]);
}
}
log.info("CSV file converted to the workbook");
return workBook;
} catch (Exception ex) {
log.error("Exception while converting csv to xls {}",ex);
}finally {
if (Objects.nonNull(workBook)) {
workBook.close();
}
}
return workBook;
}
Các vấn đề liên quan
- 1. Nhập dữ liệu CSV với Apache POI
- 2. Chuyển đổi Word sang HTML với Apache POI
- 3. Chuyển đổi bảng tính Excel sang HTML bằng thư viện Apache POI
- 4. Định dạng ngày Apache poi
- 5. Chuyển đổi XML sang CSV bằng Javascript
- 6. Chuyển hướng HTTP sang HTTPS bằng Apache
- 7. Tạo cột chỉ đọc bằng apache poi
- 8. Apache POI HWPF - sự cố khi chuyển đổi tệp doc sang pdf
- 9. Chuyển đổi JSON sang CSV
- 10. Đang cố gắng chuyển Apache POI tới Android
- 11. Số hàng POI Apache số
- 12. Sao chép Bảng Excel bằng cách sử dụng Apache POI
- 13. Tạo và thay đổi bảng Excel bằng Apache POI
- 14. Tạo biểu đồ excel bằng cách sử dụng Apache POI
- 15. Chuyển đổi DataTable sang luồng CSV
- 16. Có thể phân tích MS Word bằng Apache POI và chuyển đổi nó thành XML không?
- 17. Apache POI - nhận số là số nguyên
- 18. Apache POI dữ liệu ngoại tệ Format
- 19. Chuyển đổi XLSX sang CSV chính xác
- 20. Apache POI - Vấn đề đầu ra Docx
- 21. Apache POI XSSFColor từ mã hex
- 22. Standalone Jython: Lỗi nhập (Apache-POI)
- 23. Làm mới Bảng Pivot với Apache POI
- 24. Apache POI: SXSSFWorkbook.dispose() không tồn tại
- 25. Apache POI: Cách chèn cột vào tệp Excel
- 26. Python, chuyển danh sách và ghi sang tệp CSV
- 27. xuất sang csv wordpress
- 28. Apache POI, tạo ô mới ghi đè kiểu hàng
- 29. Sử dụng Apache POI để ghi ra XML từ Excel
- 30. Cần trợ giúp cho apache poi đọc xls với XSSF
cảm ơn nhưng csv của tôi là | được phân tách. khi tôi thay thế dấu phẩy bằng pipi, tôi sẽ nhận được một ký tự trên mỗi ô. tôi nên làm gì?? – v0ld3m0rt
Vui lòng xác nhận bạn đã sử dụng đơn giản "|" hoặc "\\ |". – Sankumarsingh
đã nhận được nó, tôi đã sử dụng "|" chỉ có nghĩa là hợp lý hoặc, tôi nên đã sử dụng "\\ |". thanks – v0ld3m0rt