1 package poi;
2 import java.io.FileInputStream;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Iterator;
6 import org.apache.poi.hssf.usermodel.HSSFCell;
7 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
8 import org.apache.poi.ss.usermodel.Cell;
9 import org.apache.poi.ss.usermodel.Row;
10 import org.apache.poi.ss.usermodel.Sheet;
11 import org.apache.poi.ss.usermodel.Workbook;
12 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
13
14 public class ReadExcel001 {
15 public static void main(String[] args) {
16 readXml("D:/test.xlsx");
17 System.out.println("-------------");
18 readXml("d:/test2.xls");
19 }
20 public static void readXml(String fileName){
21 boolean isE2007 = false; //判断是否是excel2007格式
22 if(fileName.endsWith("xlsx"))
23 isE2007 = true;
24 try {
25 InputStream input = new FileInputStream(fileName); //建立输入流
26 Workbook wb = null;
27 //根据文件格式(2003或者2007)来初始化
28 if(isE2007)
29 wb = new XSSFWorkbook(input);
30 else
31 wb = new HSSFWorkbook(input);
32 Sheet sheet = wb.getSheetAt(0); //获得第一个表单
33 Iterator<Row> rows = sheet.rowIterator(); //获得第一个表单的迭代器
34 while (rows.hasNext()) {
35 Row row = rows.next(); //获得行数据
36 System.out.println("Row #" + row.getRowNum()); //获得行号从0开始
37 Iterator<Cell> cells = row.cellIterator(); //获得第一行的迭代器
38 while (cells.hasNext()) {
39 Cell cell = cells.next();
40 System.out.println("Cell #" + cell.getColumnIndex());
41 switch (cell.getCellType()) { //根据cell中的类型来输出数据
42 case HSSFCell.CELL_TYPE_NUMERIC:
43 System.out.println(cell.getNumericCellValue());
44 break;
45 case HSSFCell.CELL_TYPE_STRING:
46 System.out.println(cell.getStringCellValue());
47 break;
48 case HSSFCell.CELL_TYPE_BOOLEAN:
49 System.out.println(cell.getBooleanCellValue());
50 break;
51 case HSSFCell.CELL_TYPE_FORMULA:
52 System.out.println(cell.getCellFormula());
53 break;
54 default:
55 System.out.println("unsuported sell type");
56 break;
57 }
58 }
59 }
60 } catch (IOException ex) {
61 ex.printStackTrace();
62 }
63 }
64 }
tomcat如果是通过windows服务启动,执行的是bin\tomcat.exe.他读取注册表中的值,而不是catalina.bat的设置.解决办法:修改注册表HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Procrun 2.0\Tomcat6\Parameters\JavaOptions原值为-Dcatalina.home=E:\Tomcat 6.0-Dcatalina.base=E:\Tomcat 6.0-Djava.endorsed.dirs=E:\Tomcat 6.0\common\endorsed-Djava.io.tmpdir=E:\Tomcat 6.0\temp-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager-Djava.util.logging.config.file=E:\Tomcat 6.0\conf\logging.properties
在后面增加即可:
-Xms128M
-Xmx512M
-XX:PermSize=128M
-XX:MaxPermSize=512M
必须要分行写。
最终效果如下: