最常用的技术e799bee5baa6e79fa5e98193e4b893e5b19e364就是pio技术。publ" />
乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>java</em>利用poi技术导入批量excel数据,并且分段存入数...-java

<em>java</em>利用poi技术导入批量excel数据,并且分段存入数...-java

作者:乔山办公网日期:

返回目录:excel表格制作





最常用的技术e799bee5baa6e79fa5e98193e4b893e5b19e364就是pio技术。
public static void readFromExcel(String file) throws IOException{
HSSFWorkbook myExcelBook = new HSSFWorkbook(new FileInputStream(file));
HSSFSheet myExcelSheet = myExcelBook.getSheet("Birthdays");
HSSFRow row = myExcelSheet.getRow(0);
if(row.getCell(0).getCellType() == HSSFCell.CELL_TYPE_STRING){
String name = row.getCell(0).getStringCellValue();
System.out.println("name : " + name);
}
if(row.getCell(1).getCellType() == HSSFCell.CELL_TYPE_NUMERIC){
Date birthdate = row.getCell(1).getDateCellValue();
System.out.println("birthdate :" + birthdate);
}
myExcelBook.close();
}

java使用第三方工具包POI技术e799bee5baa6e79fa5e98193e58685e5aeb9361实现excel数据的批量导入导出。

举例如下:

1、下载apache的相关jar包。poi-ooxml-3.6.jar xmlbeans-2.3.0.jar等,如图:

2、编写相关的读写类

    /**

     * 读取xls文件内容

     */

    private
List<XlsDto> readXls() throws
IOException {

        InputStream is = new
FileInputStream("test.xls");

        HSSFWorkbook hssfWorkbook = new
HSSFWorkbook(is);

        XlsDto xlsDto = null;

        List<XlsDto> list = new
ArrayList<XlsDto>();

        // 循环工作表Sheet

        for
(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {

            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);

            if
(hssfSheet == null) {

                continue;

            }

            // 循环行Row

            for
(int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {

                HSSFRow hssfRow = hssfSheet.getRow(rowNum);

                if
(hssfRow == null) {

                    continue;

                }

                xlsDto = new
XlsDto();

                // 循环列Cell

                // 0学号 1姓名 2学院 3课程名 4 成绩

                // for (int cellNum = 0; cellNum <=4; cellNum++) {

                HSSFCell xh = hssfRow.getCell(0);

                if
(xh == null) {

                    continue;

                }

                xlsDto.setXh(getValue(xh));

                HSSFCell xm = hssfRow.getCell(1);

                if
(xm == null) {

                    continue;

                }

                xlsDto.setXm(getValue(xm));

                HSSFCell yxsmc = hssfRow.getCell(2);

                if
(yxsmc == null) {

                    continue;

                }

                xlsDto.setYxsmc(getValue(yxsmc));

                HSSFCell kcm = hssfRow.getCell(3);

                if
(kcm == null) {

                    continue;

                }

                xlsDto.setKcm(getValue(kcm));

                HSSFCell cj = hssfRow.getCell(4);

                if
(cj == null) {

                    continue;

                }

                xlsDto.setCj(Float.parseFloat(getValue(cj)));

                list.add(xlsDto);

            }

        }

        return
list;

    } 

3、导出就是输入到一个新的excel文件里面

public void writeXls(List<Student> list, String path) throws Exception {

    if (list == null) {原始数据为空,直接返回

        return;

    }

    int countColumnNum = list.size();//设置列数

    HSSFWorkbook book = new HSSFWorkbook(); //创建工作表对象

    HSSFSheet sheet = book.createSheet("studentSheet");

    // 创建第一行

    HSSFRow firstRow = sheet.createRow(0);

    HSSFCell[] firstCells = new HSSFCell[countColumnNum];

    //创建表头

    String[] options = { "no", "name", "age", "score" };

   //循环数据域

    for (int j = 0; j < options.length; j++) {

        firstCells[j] = firstRow.createCell(j);

        firstCells[j].setCellValue(new HSSFRichTextString(options[j]));

    }

    //处理每一个cell的值

    for (int i = 0; i < countColumnNum; i++) {

        HSSFRow row = sheet.createRow(i + 1);

        Student student = list.get(i);

        for (int column = 0; column < options.length; column++) {

            HSSFCell no = row.createCell(0);

            HSSFCell name = row.createCell(1);

            HSSFCell age = row.createCell(2);

            HSSFCell score = row.createCell(3);

            no.setCellValue(student.getNo());

            name.setCellValue(student.getName());

            age.setCellValue(student.getAge());

            score.setCellValue(student.getScore());

        }

    }

    File file = new File(path);

    OutputStream os = new FileOutputStream(file);

    System.out.println(Common.WRITE_DATA + path);

    book.write(os);

    os.close();

}

相关阅读

关键词不能为空
极力推荐
  • <em>Excel</em>怎么在两个<em>工作表</em>中<

  • 可以使用Vlookup函数来实现。1、该函数的语法规则如下:VLOOKUP(lookup_value,table_array,col_index_num,range_lookup)参数说明:Lookup_value为需要在数据表第一列中进行查找的数值e69da5e6ba907a686964616f3

ppt怎么做_excel表格制作_office365_word文档_365办公网