poi 中读取和写入excel 方法

来源:互联网 发布:磁链下载软件 编辑:程序博客网 时间:2024/04/30 02:03

1 excel读取和写入的方法

package com.poi.importxls;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.formula.functions.Value;public class ImportExcelTest {public static void main(String[] args) throws Exception {FileInputStream inputStream = null;HSSFWorkbook book = null;HSSFRow row = null;HSSFCell cell = null;inputStream = new FileInputStream("d:/docs/22李荣光 - 副本.xls");book = new HSSFWorkbook(inputStream);HSSFSheet sheet = book.getSheetAt(0);int rows = sheet.getLastRowNum();System.out.println("行数:"+rows);for (int i = 0; i <= rows; i++) {if(i>=20) continue;row = sheet.getRow(i);int cols = row.getLastCellNum();System.out.println(i+"行列数"+cols);for (int j = 0; j < cols; j++) {cell = row.getCell(j);Object obj;switch (cell.getCellType()) {case HSSFCell.CELL_TYPE_NUMERIC:obj = cell.getNumericCellValue();System.out.println(j+"列"+obj);break;case HSSFCell.CELL_TYPE_STRING:obj = cell.getStringCellValue();System.out.println(j+"列"+obj);break;default:break;}}}book.write(new FileOutputStream("d:/docs/22李荣光 - 副本-2.xls"));}}

2


原创粉丝点击