Nuestro team redactor ha estado mucho tiempo investigando soluciones a tu interrogante, te dejamos la respuesta y deseamos serte de gran apoyo.
Ejemplo: convertir Excel a CSV en Java
importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.util.Iterator;importorg.apache.commons.io.FilenameUtils;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.ss.usermodel.Sheet;importorg.apache.poi.ss.usermodel.Workbook;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;publicclassXlsxtoCSVstaticvoidxlsx(File inputFile,File outputFile)// For storing data into CSV filesStringBuffer data =newStringBuffer();tryFileOutputStream fos =newFileOutputStream(outputFile);// Get the workbook object for XLSX fileFileInputStream fis =newFileInputStream(inputFile);Workbook workbook =null;String ext =FilenameUtils.getExtension(inputFile.toString());if(ext.equalsIgnoreCase("xlsx"))
workbook =newXSSFWorkbook(fis);elseif(ext.equalsIgnoreCase("xls"))
workbook =newHSSFWorkbook(fis);// Get first sheet from the workbookint numberOfSheets = workbook.getNumberOfSheets();Row row;Cell cell;// Iterate through each rows from first sheetfor(int i =0; i < numberOfSheets; i++)Sheet sheet = workbook.getSheetAt(0);Iterator<Row> rowIterator = sheet.iterator();while(rowIterator.hasNext())
row = rowIterator.next();// For each row, iterate through each columnsIterator<Cell> cellIterator = row.cellIterator();while(cellIterator.hasNext())
cell = cellIterator.next();switch(cell.getCellType())caseCell.CELL_TYPE_BOOLEAN:
data.append(cell.getBooleanCellValue()+",");break;caseCell.CELL_TYPE_NUMERIC:
data.append(cell.getNumericCellValue()+",");break;caseCell.CELL_TYPE_STRING:
data.append(cell.getStringCellValue()+",");break;caseCell.CELL_TYPE_BLANK:
data.append(""+",");break;default:
data.append(cell +",");
data.append('n');// appending new line after each row
fos.write(data.toString().getBytes());
fos.close();catch(Exception ioe)
ioe.printStackTrace();// testing the applicationpublicstaticvoidmain(String[] args)// int i=0;// reading file from desktopFile inputFile =newFile(".//src//test//resources//yourExcel.xls");//provide your path// writing excel data to csvFile outputFile =newFile(".//src//test//resources//yourCSV.csv");//provide your pathxlsx(inputFile, outputFile);System.out.println("Conversion of "+ inputFile +" to flat file: "+ outputFile +" is completed");
Sección de Reseñas y Valoraciones
Finalizando este artículo puedes encontrar las notas de otros sys admins, tú incluso tienes la libertad de dejar el tuyo si lo crees conveniente.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)