IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

【Java】导入导出Excel表格

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

1、将excel导入到内存

1、 调用工作簿Workbook的静态方法getWorkbook(),获得工作簿Workbook对象

  InputStream in = new FileInputStream(file);  
  Workbook wb = Workbook.getWorkbook(in);

2、 获取Excel表中的工作表格Sheet
3、 获取行、列
  sheet.getRows();
  sheet.getColumns();
4、 读取单元格内容
  String result = cell.getContents();
5、 关闭工作簿Workbook
  wb.close();

  • 代码演示
    // 实现读学生文件,将读出的信息存放于student集合中
    public List<Student> ReadFromExcel(String fileName) {

        List<Student> list = new ArrayList<Student>();
        File file = new File(fileName);
        try {
            InputStream in = new FileInputStream(file);
            Workbook wb = Workbook.getWorkbook(in);
            Sheet s = wb.getSheet(0);
            for(int i = 1; i < s.getRows(); i++)   //第一行不要
            {
                Cell[] row = s.getRow(i);           
                Student student = new Student(row[0].getContents(), row[1].getContents(),   //填充数据
                        row[2].getContents(), Float.parseFloat(row[3].getContents()),
                        Float.parseFloat(row[4].getContents()), Float.parseFloat(row[5].getContents()));
                //由于读取的数据全部都是String 类型所以要转换成Float类型 
                student.setTotalScore(student.getEnglish()+student.getJava()+student.getMath());
                student.setAverage(student.getTotalScore()/3);
                list.add(student);                      
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BiffException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return list;
    }

2、将数据写到excel表格中

1、 获取可写入工作簿WritableWorkbook对象
  WritableWorkbook wwb = Workbook.createWorkbook(filename);
2、 创建工作表格Sheet名称
  WritableSheet sheet = book.createSheet("Sheet1",0);
3、 将内容放入对应的行和列

  sheet.addCell(new Label(j, i, info);//j表示列,i表示行,info表示写入的内容   
      //在Excel中第一个参数是列,第二个参数是行,如A1

4、 写入并关闭工作簿Workbook

  wwb.write();
  wwb.close();
  • 代码演示
    // 将集合中的数据写入到excel文件中
    public void WriteExcel(List<Student> list, String fileName) {
        File file = new File(fileName);
        try {
            OutputStream out = new FileOutputStream(file);
            WritableWorkbook wwb = Workbook.createWorkbook(out);
            WritableSheet ws = wwb.createSheet("Sheet1", 0);
            String info[] = {"id","name","gender","java","english","math"};
            for(int j=0;j<6;j++){
                Label label = new Label(j, 0, info[j]);
                ws.addCell(label);
            }
            for(int i = 0;i < list.size();i++)
            {
                Label l = new Label(0, i+1, list.get(i).getId());//在Excel中,第一个参数表示列,第二个表示行
                Label l2 = new Label(1, i+1, list.get(i).getName());
                Label l3 = new Label(2, i+1, list.get(i).getGender());
                Label l4 = new Label(3, i+1, String.valueOf(list.get(i).getJava()));
                Label l5 = new Label(4, i+1, String.valueOf(list.get(i).getEnglish()));
                Label l6 = new Label(5, i+1, String.valueOf(list.get(i).getMath()));
                ws.addCell(l);
                ws.addCell(l2);
                ws.addCell(l3);
                ws.addCell(l4);
                ws.addCell(l5);
                ws.addCell(l6);
            }

            wwb.write();//从内存中写入文件中
            wwb.close();//关闭资源,释放内存
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RowsExceededException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

文章永久链接:https://tech.souyunku.com/?p=20037


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(71) 打赏



未经允许不得转载:搜云库技术团队 » 【Java】导入导出Excel表格

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367