Java导入Excel表格,并将数据存入数组中

来源:互联网 发布:mac显卡性能测试软件 编辑:程序博客网 时间:2024/06/06 01:30

首先在项目中导入poi-3.17.jar和poi-ooxml3.17.jar两个包。

!!!注意Excel后缀为.xls

代码如下:

package package1;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.util.Iterator;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.poifs.filesystem.POIFSFileSystem;import org.apache.poi.sl.usermodel.Sheet;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.CellType;import org.apache.poi.ss.usermodel.Row;public class test1 {public static void read(String fileName) {String[][] t1Array = new String[835][4];try{InputStream input=new FileInputStream(fileName);  //建立输入流HSSFWorkbook wb=new HSSFWorkbook(input);//初始化HSSFSheet sheet=wb.getSheetAt(0);//获得第一个表单//总行数int rowLength=sheet.getLastRowNum()+1;HSSFRow hssfRow=sheet.getRow(0);//得到Excel工作表的行int colLength=hssfRow.getLastCellNum();//总列数  HSSFCell hssfCell=hssfRow.getCell(0);//得到Excel指定单元格中的内容CellStyle cellStyle=hssfCell.getCellStyle();//得到单元格样式for(int i=1;i<rowLength;i++)  //去掉表头{HSSFRow row=sheet.getRow(i);//获取Excel工作表的每行for(int j=0;j<colLength;j++){HSSFCell cell=row.getCell(j);//获取指定单元格if(cell!=null)//将所有的需要读的Cell表格设置为String格式{cell.setCellType(CellType.STRING);}if(j>0){t1Array[i-1][j-1]=cell.getStringCellValue(); }//System.out.println(cell.getStringCellValue());  //打印每行单元格中的数据}System.out.println(t1Array[i-1][0]+" "+t1Array[i-1][1]+" "+t1Array[i-1][2]+" "+t1Array[i-1][3]);//打印数组中的值}}catch(IOException e){e.printStackTrace();}}public static void main(String[] args) throws IOException{read("G:\\data\\fu1.xls");}}