Reading Excel files in Java (simple JSP and servlet)

how to read an excel sheet in java without processing it as a database and without using any other external api.

+3
source share
5 answers

You can convert the xls file to a csv file . Java API supports csv files. You can read the csv file using standrt I / O libraries.

+1
source

There is no direct way to work with an Excel worksheet in Java. You must use the Apache POI Java API.

Apache POI is a Java library for reading and writing various Microsoft file formats, especially those related to Office, based on OLE2 and OOXML, such as XLS and DOCX.

Excel. xls xlsx.

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;
}

, a Vector .

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

. Vector . , Java. , Java Collection.

+4

.

API- Java SE EE Excel.

(, , Excel . . , .)


Re: CSV , :

  • .
  • CSV (, Excel).
  • . . .
+1
source

You must use Apache POI to read xls and xlsx files

You can use HSSF, XSSF, SXSSF according to your memeory restrictions ....

+1
source

try it

    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();
        }

    }
0
source

All Articles