Necesito leer un archivo Excel 2007 XLSX en una aplicación Java. ¿Alguien sabe de una buena API para realizar esta tarea?Leer el archivo XLSX en Java
Respuesta
yo sepa no hay xlsx-bibliotecas disponibles todavía. Pero hay algunos para xls antiguos:
Una biblioteca es jxls que utiliza internamente el ya mencionado POI.
2 otros enlaces: Handle Excel files, Java libraries to read and write Excel XLS document files.
Tuve que hacer esto en .NET y no pude encontrar ninguna API. Mi solución fue descomprimir el .xlsx y sumergirme en la manipulación del XML. No es tan malo una vez que creas tus clases de ayudantes y tal.
Hay algunos "errores" como los nodos, todos tienen que ser ordenados de acuerdo con la forma en que Excel los espera, que no encontré en los documentos oficiales. Excel tiene su propia marca de fecha y hora, por lo que deberá crear una fórmula de conversión.
¿Has mirado el poorly obfuscated API?
importa:
HSSF es la implementación Java pura del Proyecto POI del formato '97 (-2007) archivo de Excel. No admite el nuevo formato de archivo .xlsx OOXML de Excel 2007, que no está basado en OLE2.
En su lugar, puede considerar el uso de JDBC-ODBC bridge.
Éste quizás funcione para usted, puede leer/escribir el archivo xlsx de Excel 2007. SmartXLS
No estoy muy contento con ninguna de las opciones, así que terminé solicitando el archivo en formato Excel 97. El POI funciona muy bien para eso. Gracias a todos por la ayuda.
Podría ser un poco tarde, pero el POI beta ahora es compatible con xlsx.
Apache POI 3.5 han añadido soporte a todo el OOXML (docx, xlsx, etc.)
Véase el docx4j XSSF sub project
ahora cubre xlsx también.
"¿Por qué usaría docx4j para hacer esto", le escuché preguntar, "en lugar de POI, que se enfoca en xlsx y xls binarios?"
Probablemente porque te gusta JAXB (en contraposición a XML Beans), o ya estás usando docx4j para docx o pptx, y también debes poder hacer algunas cosas con xlsx.
Otra razón posible es que el jar XML Beans generado a partir de los esquemas de OpenXML es demasiado grande para sus propósitos. (Para evitar esto, POI ofrece un subconjunto 'lite': el 'gran' ooxml-schemas-1.0.jar es de 14.5 MB! Pero si necesita soportar hojas de cálculo arbitrarias, probablemente necesite el jar completo). Por el contrario, la totalidad de docx4j/pptx4j/xlsx4j pesa aproximadamente lo mismo que el subconjunto lite de POI.
Si está procesando hojas de cálculo solamente (es decir, no docx o pptx), y el párrafo anterior no es una preocupación para usted, entonces probablemente sea mejor que use POI.
Aspose.Cells for Java admite el formato XLSX. Puede encontrar más detalles y más ayuda en Aspose.Cells for Java Documentation. Por favor mira si esto ayuda.
Divulgación: trabajo como desarrollador evangelista en Aspose.
Prueba esto: el archivo
- Descomprimir XLSX
- leer archivos XML
- datos de redacción y uso
código Ejemplo:
public Workbook getTemplateData(String xlsxFile) {
Workbook workbook = new Workbook();
parseSharedStrings(xlsxFile);
parseWorkesheet(xlsxFile, workbook);
parseComments(xlsxFile, workbook);
for (Worksheet worksheet : workbook.sheets) {
worksheet.dimension = manager.getDimension(worksheet);
}
return workbook;
}
private void parseComments(String tmpFile, Workbook workbook) {
try {
FileInputStream fin = new FileInputStream(tmpFile);
final ZipInputStream zin = new ZipInputStream(fin);
InputStream in = getInputStream(zin);
while (true) {
ZipEntry entry = zin.getNextEntry();
if (entry == null)
break;
String name = entry.getName();
if (name.endsWith(".xml")) { //$NON-NLS-1$
if (name.contains(COMMENTS)) {
parseComments(in, workbook);
}
}
zin.closeEntry();
}
in.close();
zin.close();
fin.close();
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
e.printStackTrace();
}
}
private void parseComments(InputStream in, Workbook workbook) {
try {
DefaultHandler handler = getCommentHandler(workbook);
SAXParser saxParser = getSAXParser();
saxParser.parse(in, handler);
} catch (Exception e) {
e.printStackTrace();
}
}
private DefaultHandler getCommentHandler(Workbook workbook) {
final Worksheet ws = workbook.sheets.get(0);
return new DefaultHandler() {
String lastTag = "";
private Cell ccell;
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes) throws SAXException {
lastTag = qName;
if (lastTag.equals("comment")) {
String cellName = attributes.getValue("ref");
int r = manager.getRowIndex(cellName);
int c = manager.getColumnIndex(cellName);
Row row = ws.rows.get(r);
if (row == null) {
row = new Row();
row.index = r;
ws.rows.put(r, row);
}
ccell = row.cells.get(c);
if (ccell == null) {
ccell = new Cell();
ccell.cellName = cellName;
row.cells.put(c, ccell);
}
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String val = "";
if (ccell != null && lastTag.equals("t")) {
for (int i = start; i < start + length; i++) {
val += ch[i];
}
if (ccell.comment == null)
ccell.comment = val;
else {
ccell.comment += val;
}
}
}
};
}
private void parseSharedStrings(String tmpFile) {
try {
FileInputStream fin = new FileInputStream(tmpFile);
final ZipInputStream zin = new ZipInputStream(fin);
InputStream in = getInputStream(zin);
while (true) {
ZipEntry entry = zin.getNextEntry();
if (entry == null)
break;
String name = entry.getName();
if (name.endsWith(".xml")) { //$NON-NLS-1$
if (name.startsWith(SHARED_STRINGS)) {
parseStrings(in);
}
}
zin.closeEntry();
}
in.close();
zin.close();
fin.close();
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
e.printStackTrace();
}
}
public void parseWorkesheet(String tmpFile, Workbook workbook) {
try {
FileInputStream fin = new FileInputStream(tmpFile);
final ZipInputStream zin = new ZipInputStream(fin);
InputStream in = getInputStream(zin);
while (true) {
ZipEntry entry = zin.getNextEntry();
if (entry == null)
break;
String name = entry.getName();
if (name.endsWith(".xml")) { //$NON-NLS-1$
if (name.contains("worksheets")) {
Worksheet worksheet = new Worksheet();
worksheet.name = name;
parseWorksheet(in, worksheet);
workbook.sheets.add(worksheet);
}
}
zin.closeEntry();
}
in.close();
zin.close();
fin.close();
} catch (FileNotFoundException e) {
System.out.println(e);
} catch (IOException e) {
e.printStackTrace();
}
}
public void parseWorksheet(InputStream in, Worksheet worksheet)
throws IOException {
// read sheet1 sharedStrings
// styles, strings, formulas ...
try {
DefaultHandler handler = getDefaultHandler(worksheet);
SAXParser saxParser = getSAXParser();
saxParser.parse(in, handler);
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
donde la clase libro de trabajo:
public class Workbook {
Integer id = null;
public List<Worksheet> sheets = new ArrayList<Worksheet>();}
y la hoja de trabajo de clase:
public class Worksheet {
public Integer id = null;
public String name = null;
public String dimension = null;
public Map<Integer, Row> rows = new TreeMap<Integer, Row>();
public Map<Integer, Column> columns = new TreeMap<Integer, Column>();
public List<Span> spans = new ArrayList<Span>();}
y clase Row:
public class Row {
public Integer id = null;
public Integer index = null;
public Row tmpRow = null;
public Style style = null;
public Double height = null;
public Map<Integer,Cell> cells = new TreeMap<Integer, Cell>();
public String spans = null;
public Integer customHeight = null;}
y clase Cell:
public class Cell {
public Integer id = null;
public Integer rowIndex = null;
public Integer colIndex = null;
public String cellName = null;
public String text = null;
public String formula = null;
public String comment = null;
public Style style = null;
public Object value = null;
public Cell tmpCell = null;}
y clase Columna:
public class Column {
public Integer index = null;
public Style style = null;
public String width = null;
public Column tmpColumn = null;
}
y span class:
public class Span {
Integer id = null;
String topLeft = null;
String bottomRight = null;
}
¿Qué son las clases 'Span' y' Column'? – HyperNeutrino
ver la nueva versión – Koss
Puede utilizar Apache Tika para ello:
String parse(File xlsxFile) {
return new Tika().parseToString(xlsxFile);
}
Tika utiliza Apache POI para analizar archivos XLSX.
Aquí hay algunos usage examples para Tiki.
Alternativamente, si desea manejar cada celda de la hoja de cálculo de forma individual, esto es una manera de hacer esto con PDI:
void parse(File xlsx) {
try (XSSFWorkbook workbook = new XSSFWorkbook(xlsx)) {
// Handle each cell in each sheet
workbook.forEach(sheet -> sheet.forEach(row -> row.forEach(this::handle)));
}
catch (InvalidFormatException | IOException e) {
System.out.println("Can't parse file " + xlsx);
}
}
void handle(Cell cell) {
final String cellContent;
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
cellContent = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
cellContent = String.valueOf(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
cellContent = String.valueOf(cell.getBooleanCellValue());
break;
default:
cellContent = "Don't know how to handle cell " + cell;
}
System.out.println(cellContent);
}
- 1. Cómo leer el archivo XLSX de tamaño> 40MB
- 2. PHP Leer archivo xlsx Excel 2007
- 3. ¿Cómo puedo leer archivos .xlsx y .xls en Java?
- 4. leer datos de XLSX en C#
- 5. Procesando archivo xlsx grande
- 6. Descargue el archivo .xlsx usando Response.TransmitFile()
- 7. Lea el archivo excel xlsx usando simplexlsx en php
- 8. leer el archivo db SQLite usando java
- 9. Java: leer un archivo en una matriz
- 10. leer el archivo en classpath
- 11. ¿Cómo leer el archivo .class?
- 12. Cómo leer los archivos xls y xlsx usando C#
- 13. Convertir el archivo xlsx a csv usando el lote
- 14. Java: Leer matriz de enteros de archivo
- 15. JAVA: leer y escribir un archivo juntos
- 16. ¿Cómo leer un archivo XML con Java?
- 17. java archivo de leer desde el dispositivo a la red
- 18. Lectura de archivos xlsx en PHP
- 19. ¿Cómo leer un archivo en una cadena en java?
- 20. Leer siguiente palabra en Java
- 21. Escribir un archivo en formato xlsx usando Apache POI
- 22. Leer archivo en matriz
- 23. leer el archivo xml usando http
- 24. al leer el archivo app.config
- 25. ¿Cómo leer un archivo del jar en Java?
- 26. C# leer continuamente el archivo
- 27. ¿Cómo leer el protocolo helado en Java?
- 28. Cómo leer el archivo XML en Android
- 29. ¿Cómo leer el archivo XML en C#?
- 30. ¿Cómo leer todo el archivo en Ruby?
Eche un vistazo también a este: https://code.google.com/p/sjxslx/ Parece que no se mantiene desde enero de 2013, pero me encantó el código. Es genial para la transmisión de grandes archivos de Excel: carga fila por fila en la memoria + las cadenas compartidas, no toda la hoja de cálculo. –