J'essaye de convertir . Xlsx fichier en . Csv, la conversion est en cours mais les données ne sont pas formatées correctement. Veuillez trouver le code ci-dessous et suggérer des modifications du code.
Ici, j'essaie de lire un fichier . Xlsx et de l'écrire dans un fichier csv, c'est-à-dire en convertissant xlsx en csv mais je n'obtiens pas le fichier . csv au format approprié, toutes les données sont affichées en un seul mais elles doivent s'afficher comme des lignes dans Excel.
import Java.io.File;
import Java.io.FileInputStream;
import Java.io.FileOutputStream;
import Java.util.Iterator;
import org.Apache.poi.ss.usermodel.Cell;
import org.Apache.poi.ss.usermodel.Row;
import org.Apache.poi.xssf.usermodel.XSSFSheet;
import org.Apache.poi.xssf.usermodel.XSSFWorkbook;
public class XlsxtoCSV {
static void xlsx(File inputFile, File outputFile) {
// For storing data into CSV files
StringBuffer data = new StringBuffer();
try {
FileOutputStream fos = new FileOutputStream(outputFile);
// Get the workbook object for XLSX file
XSSFWorkbook wBook = new XSSFWorkbook(new FileInputStream(inputFile));
// Get first sheet from the workbook
XSSFSheet sheet = wBook.getSheetAt(0);
Row row;
Cell cell;
// Iterate through each rows from first sheet
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
data.append(cell.getBooleanCellValue() + ",");
break;
case Cell.CELL_TYPE_NUMERIC:
data.append(cell.getNumericCellValue() + ",");
break;
case Cell.CELL_TYPE_STRING:
data.append(cell.getStringCellValue() + ",");
break;
case Cell.CELL_TYPE_BLANK:
data.append("" + ",");
break;
default:
data.append(cell + ",");
}
}
}
fos.write(data.toString().getBytes());
fos.close();
} catch (Exception ioe) {
ioe.printStackTrace();
}
}
//testing the application
public static void main(String[] args) {
//reading file from desktop
File inputFile = new File("C:\\Users\\user69\\Desktop\\test.xlsx");
//writing Excel data to csv
File outputFile = new File("C:\\Users\\user69\\Desktop\\test1.csv");
xlsx(inputFile, outputFile);
}
}
C'est merci @Swapnil!
data.append("\r\n"); // After the columns have been appended.
Ce qui suit a été édité (ajouté) par @Abdullah Ma réponse originale ci-dessus n'a pas beaucoup d'impact, mais l'édition d'Abdullah montre beaucoup d'efforts, donc je laisse cela à ceux qui rencontrent cette question et réponse.
public class App {
public void convertExcelToCSV(Sheet sheet, String sheetName) {
StringBuilder data = new StringBuilder();
try {
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
CellType type = cell.getCellTypeEnum();
if (type == CellType.BOOLEAN) {
data.append(cell.getBooleanCellValue());
} else if (type == CellType.NUMERIC) {
data.append(cell.getNumericCellValue());
} else if (type == CellType.STRING) {
data.append(cell.getStringCellValue());
} else if (type == CellType.BLANK) {
} else {
data.append(cell + "");
}
data.append(",");
}
data.append('\n');
}
Files.write(Paths.get("C:\\Users\\" + sheetName + ".csv"),
data.toString().getBytes("UTF-8"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String [] args)
{
App app = new App();
String path = "C:\\Users\\myFile.xlsx";
InputStream inp = null;
try (InputStream inp = new FileInputStream(path)) {
Workbook wb = WorkbookFactory.create(inp);
for (int i = 0; i < wb.getNumberOfSheets(); i++) {
System.out.println(wb.getSheetAt(i).getSheetName());
app.convertExcelToCSV(wb.getSheetAt(i), wb.getSheetAt(i).getSheetName());
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Utilisez Commons CSV pour encoder les valeurs des cellules, c'est beaucoup plus robuste. Malheureusement, un code supplémentaire est encore nécessaire pour parcourir la feuille par ligne/cellule et appeler Commons CSV sur chaque cellule (XSSF ne le fournit pas), mais au moins la valeur réelle de la cellule en cours d'écriture est garantie d'être un CSV standard (c.-à-d. vous n'avez pas à vous soucier d'échapper des caractères ou d'ajouter des virgules vous-même).
Ajout de Maven pour Commons CSV:
<dependency>
<groupId>org.Apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.5</version>
</dependency>
Une fois que Commons CSV est disponible, il s'agit du code pour exporter un classeur au format CSV. Cet exemple écrit dans un OutputStream, mais la sortie de fichier est également facile.
// Convert an XSSFWorkbook to CSV and write to provided OutputStream
private void writeWorkbookAsCSVToOutputStream(XSSFWorkbook workbook, OutputStream out) {
CSVPrinter csvPrinter = null;
try {
csvPrinter = new CSVPrinter(new OutputStreamWriter(out), CSVFormat.DEFAULT);
if (workbook != null) {
XSSFSheet sheet = workbook.getSheetAt(0); // Sheet #0 in this example
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
csvPrinter.print(cell.getStringCellValue());
}
csvPrinter.println(); // Newline after each row
}
}
}
catch (Exception e) {
log.error("Failed to write CSV file to output stream", e);
}
finally {
try {
if (csvPrinter != null) {
csvPrinter.flush(); // Flush and close CSVPrinter
csvPrinter.close();
}
}
catch (IOException ioe) {
log.error("Error when closing CSV Printer", ioe);
}
}
}
public static void convertToXlsx(File inputFile, File outputFile) {
StringBuffer bf = new StringBuffer();
FileOutputStream fos = null;
String strGetValue = "";
try {
fos = new FileOutputStream(outputFile);
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile));
XSSFSheet sheet = wb.getSheetAt(0);
Row row;
Cell cell;
int intRowCounter = 0;
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
StringBuffer cellDData = new StringBuffer();
row = rowIterator.next();
int maxNumOfCells = sheet.getRow(0).getLastCellNum();
int cellCounter = 0;
while ((cellCounter) < maxNumOfCells) {
if (sheet.getRow(row.getRowNum()) != null
&& sheet.getRow(row.getRowNum()).getCell(cellCounter) != null) {
cell = sheet.getRow(row.getRowNum()).getCell(cellCounter);
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BOOLEAN:
strGetValue = cell.getBooleanCellValue() + ",";
cellDData.append(removeSpace(strGetValue));
break;
case Cell.CELL_TYPE_NUMERIC:
strGetValue = new BigDecimal(cell.getNumericCellValue()).toPlainString();
if (DateUtil.isCellDateFormatted(cell)) {
strGetValue = new DataFormatter().formatCellValue(cell);
} else {
strGetValue = new BigDecimal(cell.getNumericCellValue()).toPlainString();
}
String tempStrGetValue = removeSpace(strGetValue);
if (tempStrGetValue.length() == 0) {
strGetValue = " ,";
cellDData.append(strGetValue);
} else {
strGetValue = strGetValue + ",";
cellDData.append(removeSpace(strGetValue));
}
break;
case Cell.CELL_TYPE_STRING:
strGetValue = cell.getStringCellValue();
String tempStrGetValue1 = removeSpace(strGetValue);
if (tempStrGetValue1.length() == 0) {
strGetValue = " ,";
cellDData.append(strGetValue);
} else {
strGetValue = strGetValue + ",";
cellDData.append(removeSpace(strGetValue));
}
break;
case Cell.CELL_TYPE_BLANK:
strGetValue = "" + ",";
cellDData.append(removeSpace(strGetValue));
break;
default:
strGetValue = cell + ",";
cellDData.append(removeSpace(strGetValue));
}
} else {
strGetValue = " ,";
cellDData.append(strGetValue);
}
cellCounter++;
}
String temp = cellDData.toString();
if (temp != null && temp.contains(",,,")) {
temp = temp.replaceFirst(",,,", ", ,");
}
if (temp.endsWith(",")) {
temp = temp.substring(0, temp.lastIndexOf(","));
cellDData = null;
bf.append(temp.trim());
}
bf.append("\n");
intRowCounter++;
}
fos.write(bf.toString().getBytes());
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (fos != null)
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
private static String removeSpace(String strString) {
if (strString != null && !strString.equals("")) {
return strString.trim();
}
return strString;
}