package com.accp.struts.action;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.record.*;
import org.apache.poi.hssf.model.*;
import org.apache.poi.hssf.util.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import java.io.FileInputStream;
public class Xls2table {
public static String fileToBeRead = "c://1.xls";
public static void readFile0() {
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
fileToBeRead));
HSSFSheet sheet =workbook.getSheet(workbook.getSheetName(0));
int rows = sheet.getPhysicalNumberOfRows();
for (int r = 0; r < rows; r++) {
HSSFRow row = sheet.getRow(r);
if (row != null) {
int cells = row.getPhysicalNumberOfCells();
String value = "";
System.out.println(cells);
for (short c = 0; c < cells; c++) {
HSSFCell cell = row.getCell(c);
if (cell != null) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
//
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value += (long) cell.getNumericCellValue()
+ "/t";
break;
case HSSFCell.CELL_TYPE_STRING:
value += cell.getStringCellValue() + "/t";
break;
default:
value += "/t";
}
}
}
// 下面可以将查找到的行内容用SQL语句INSERT到oracle
System.out.println(value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}