Nuestro team de trabajo ha estado mucho tiempo buscando la resolución a tu interrogante, te brindamos la resolución así que nuestro objetivo es servirte de gran 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");
Te mostramos reseñas y calificaciones
Finalizando este artículo puedes encontrar las interpretaciones de otros creadores, tú además puedes dejar el tuyo si te apetece.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)