3

如何在不将其视为数据库且不使用任何其他外部 api 的情况下在 java 中读取 Excel 工作表。

4

5 回答 5

4

在 Java 中没有直接处理 Excel 工作表的方法。您应该使用Apache POI Java API。

Apache POI 是一个 Java 库,用于读写各种 Microsoft 文件格式,尤其是 Office 相关的文件格式,基于 OLE2 和 OOXML,例如 XLS 和 DOCX。

让我们看一个阅读 Excel 工作表的示例。它同时支持xlsxlsx文件格式。

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

private Vector importExcelSheet(String fileName)
{
    Vector cellVectorHolder = new Vector();
    try
    {
        Workbook workBook = WorkbookFactory.create(new FileInputStream(fileName));
        Sheet sheet = workBook.getSheetAt(0);
        Iterator rowIter = sheet.rowIterator();

        while(rowIter.hasNext())
        {
            XSSFRow row = (XSSFRow) rowIter.next();
            Iterator cellIter = row.cellIterator();
            Vector cellStoreVector=new Vector();

            while(cellIter.hasNext())
            {
                XSSFCell cell = (XSSFCell) cellIter.next();
                cellStoreVector.addElement(cell);
            }
            cellVectorHolder.addElement(cellStoreVector);
        }
    }
    catch (Exception e)
    {
        System.out.println(e.getMessage());
    }
    return cellVectorHolder;
}

调用上述方法,该方法将返回 aVector如下。

Vector dataHolder=importExcelSheet("Excel_file.xlsx");

注意:Vector这里使用的只是为了演示。不应该使用它,因为它在 Java 中已经过时了。使用 Java 集合框架中可用的其他类型的集合。

于 2012-04-21T09:54:04.380 回答
1

您必须使用Apache POI来读取 xls 和 xlsx 文件

你可以根据你的内存限制使用 HSSF、XSSF、SXSSF....

于 2012-04-21T09:41:39.783 回答
1

You can't.

The Java SE and EE APIs do not support the reading of Excel spreadsheets.

(Well, I suppose you could spend a few months reading the Excel specs and developing your own spreadsheet reader from scratch. But that strikes me as waste of effort. Just use one of the alternatives that you have rejected.)


Re: the approach of converting the spreadsheet to a CSV and reading that:

  • This is NOT reading the spreadsheet.
  • It requires an external application (e.g Excel) to do the conversion from the spreadsheet to CSV.
  • It is lossy. You can only read the simple data content of the spreadsheet. Everything else is lost.
于 2012-04-21T09:17:40.323 回答
1

您可以将xls文件转换为csv文件。Java API 支持 csv 文件。您可以使用标准 I/O 库读取 csv 文件。

于 2012-04-21T09:20:19.910 回答
0

尝试这个

    import java.io.File;
    import java.io.IOException;

    import jxl.Cell;
    import jxl.CellType;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;

    public class ReadExcel {

   private String inputFile;

   public void setInputFile(String inputFile) {
    this.inputFile = inputFile;
        }

   public void read() throws IOException  {
    File inputWorkbook = new File(inputFile);
  Workbook w;
try {
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines
for (int j = 0; j < sheet.getColumns(); j++) {
for (int i = 0; i < sheet.getRows(); i++) {
Cell cell = sheet.getCell(j, i);
CellType type = cell.getType();
if (cell.getType() == CellType.LABEL) {
System.out.println("I got a label "
                    + cell.getContents());
 }
  if (cell.getType() == CellType.NUMBER) {
System.out.println("I got a number "
                    + cell.getContents());
 }
 }
}
} catch (BiffException e) {
 e.printStackTrace();
}
}

public static void main(String[] args) throws IOException {
ReadExcel test = new ReadExcel();
test.setInputFile("c:/temp/lars.xls");
test.read();
        }

    }
于 2012-04-21T09:29:58.153 回答