Este equipo de redactores ha estado mucho tiempo investigando para darle resolución a tu interrogante, te brindamos la resolución por esto nuestro objetivo es que te resulte de mucha apoyo.
Ejemplo: convertir Excel a CSV en Java
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.Iterator;import org.apache.commons.io.FilenameUtils;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 org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public classXlsxtoCSV
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 objectfor XLSX file
FileInputStream fis = new FileInputStream(inputFile);
Workbook workbook = null;
String ext = FilenameUtils.getExtension(inputFile.toString());if(ext.equalsIgnoreCase("xlsx"))
workbook = new XSSFWorkbook(fis);elseif(ext.equalsIgnoreCase("xls"))
workbook = new HSSFWorkbook(fis);// Get first sheet from the workbook
int numberOfSheets = workbook.getNumberOfSheets();
Row row;
Cell cell;// Iterate through each rows from first sheet
for(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 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 +",");
data.append('n');// appending new line after each row
fos.write(data.toString().getBytes());
fos.close(); catch (Exception ioe)
ioe.printStackTrace();// testing the application
public static void main(String[] args)//int i=0;// reading filefrom desktop
File inputFile = new File(".//src//test//resources//yourExcel.xls");//provide your path
// writing excel data to csv
File outputFile = new File(".//src//test//resources//yourCSV.csv");//provide your path
xlsx(inputFile, outputFile);
System.out.println("Conversion of "+ inputFile +" to flat file: "+ outputFile +" is completed");
Calificaciones y reseñas
Finalizando este artículo puedes encontrar las explicaciones de otros gestores de proyectos, tú además eres capaz insertar el tuyo si lo deseas.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)