欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JAVA导入导出Excel

程序员文章站 2024-02-24 14:35:22
...

导入导出Excel有jxl和poi两种方式

这里介绍poi方式

需要准备的jar包

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.17</version>
        </dependency>

poi 含有解析Excel 2003的类(HSSFWorkbook)

poi-ooxml 含有解析Excel2007+的类(XSSFWorkbook)

读取Excel简单例子

@RequestMapping("/excelParse")
@ResponseBody
public List<Excel> excelParse(MultipartFile pfile,HttpServletRequest request) {

        List<Excel> excelList = new ArrayList<Excel>(10);
        System.out.println(pfile.getOriginalFilename());
        try {
            //Excel2003后缀名为.xls  endsWith用来判断Excel类型
            if(pfile.getOriginalFilename().endsWith("xls")){
                //poi--exl解析
                InputStream is = pfile.getInputStream();
                HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
                HSSFSheet sheet = hssfWorkbook.getSheet("Sheet1");
                for (int i = 1; i < sheet.getLastRowNum(); i++) {
                    HSSFCell riid1 = sheet.getRow(i).getCell(0);
                    HSSFCell riid2 = sheet.getRow(i).getCell(1);
                    HSSFCell riid3 = sheet.getRow(i).getCell(2);
                    HSSFCell riid4 = sheet.getRow(i).getCell(3);
                    HSSFCell riid5 = sheet.getRow(i).getCell(4);

                    System.out.println(riid1 + "\t" + riid2 + "\t" + riid3 + "\t" +riid4 + "\t" + riid5);
                    System.out.println("---------------------------------");

                    //自定义Excel类来封装读取到的Excel数据
                    Excel excel = new Excel();
                    excel.setRiid1(riid1.toString());
                    excel.setRiid2(riid2.toString());
                    excel.setRiid3(riid3.toString());
                    excel.setRiid4(riid4.toString());
                    excel.setRiid5(riid5.toString());
                    excelList.add(excel);
                }
            }else if(pfile.getOriginalFilename().endsWith("xlsx")){//Excel2007+类型为.xlsx
                InputStream is = pfile.getInputStream();
                XSSFWorkbook xssfWorkbook = new XSSFWorkbook(is);
                XSSFSheet sheet = xssfWorkbook.getSheet("Sheet1");
                for (int i = 1; i < sheet.getLastRowNum(); i++) {
                    XSSFCell riid1 = sheet.getRow(i).getCell(0);
                    XSSFCell riid2 = sheet.getRow(i).getCell(1);
                    XSSFCell riid3 = sheet.getRow(i).getCell(2);
                    XSSFCell riid4 = sheet.getRow(i).getCell(3);
                    XSSFCell riid5 = sheet.getRow(i).getCell(4);

                    System.out.println(riid1 + "\t" + riid2 + "\t" + riid3 + "\t" +riid4 + "\t" + riid5);
                    System.out.println("-----------------------------------------------------------------------------");

                    Excel excel = new Excel();
                    excel.setRiid1(riid1.toString());
                    excel.setRiid2(riid2.toString());
                    excel.setRiid3(riid3.toString());
                    excel.setRiid4(riid4.toString());
                    excel.setRiid5(riid5.toString());
                    excelList.add(excel);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return excelList;
    }

导出excel并下载

    /**
    *  这里模拟了Excel2003的导出下载过程
    *  如果要导出Excel2007+ 请使用XSSF...(HSSF-->XSSF)类操作
    */
    @RequestMapping("/exportExcel")
    @ResponseBody
    public void exportExcel(){
        //新建文件输出流对象
        FileOutputStream out = null;
        try {
            //在桌面建立一个Excel2003文件
            out = new FileOutputStream("C:/Users/Administrator/Desktop/excel2003.xls");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //新建workbook
        HSSFWorkbook wb = new HSSFWorkbook();
        //新建sheet
        HSSFSheet sheet = wb.createSheet();
        //新建行  行从坐标0开始 这里是第三行
        HSSFRow row = sheet.createRow(2);
        //设置行高
        row.setHeightInPoints(20);
        //新建单元格  列从坐标0开始  这里是第三列
        HSSFCell cell = row.createCell(2);

        //创建整个文本的字体对象,workbook创建
        HSSFFont cnFont = wb.createFont();
        //设置字体行高,字体名字
        cnFont.setFontHeightInPoints((short)10);
        cnFont.setFontName("隶书");

        //将文本字面格式用到单元格上,新建单元格风格
        HSSFCellStyle cnStyle = wb.createCellStyle();
        cnStyle.setFont(cnFont);
        cell.setCellStyle(cnStyle);

        //单元格内文本对象新建,HSSFRichTextString的应用
        HSSFRichTextString richText = new HSSFRichTextString("中文");
        cell.setCellValue(richText);


        //再建一个单元格,重复上面的设置
        HSSFCell enCell = row.createCell(3);
        HSSFFont enFont = wb.createFont();
        enFont.setFontHeightInPoints((short) 10);
        enFont.setFontName("Arial Black");
        HSSFCellStyle enStyle = wb.createCellStyle();
        enStyle.setFont(enFont);
        enCell.setCellStyle(enStyle);
        enCell.setCellValue(new HSSFRichTextString("English"));
        //单元格列宽
        sheet.setColumnWidth(2, 4000);
        sheet.setColumnWidth(3, 4000);

        //输出
        //设置边框
        sheet.setDisplayGridlines(true);
        //设置打印的边框
        sheet.setPrintGridlines(false);

        //设置打印对象
        HSSFPrintSetup printSetup = sheet.getPrintSetup();
        //设置页边距
        printSetup.setHeaderMargin((double) 0.44); // 页眉
        printSetup.setFooterMargin((double) 0.2);//页脚

        //设置页宽
        printSetup.setFitWidth((short)1);
        printSetup.setFitHeight((short)1000);

        //设置打印方向,横向就是true
        printSetup.setLandscape(false);
        //设置A4纸
        printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);

        //打印,关闭流
        try {
            wb.write(out);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("导出Excel2003成功");
    }

poi导入导出Excel还有已经封装好的通用类

如需使用,请自行搜索

这里只提供一个poi导入导出Excel的简单示例