zoukankan      html  css  js  c++  java
  • 工具类篇——Java之Excel导入生成

    在程序员进行系统开发工作时,常常需要添加一个报表导入导出的功能,本文将详细介绍java怎么实现生成excel文件到本地。

    一、在eclipse开发工具中新建java project项目取名ExcelOpration,导入poi需要的四个jar包,poi.jar , poi-ooxml.jar , poi-ooxml-schemas.jar , xmlbeans.jar,项目结构如下:

        

    二、新建java类com.excel.ExportExcelFile.java,代码如下:

    package com.excel;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.util.CellRangeAddress;
    
    /**
     * 导出数据为excel文件
     * @author Administrator
     *
     */
    public class ExportExcelFile {public static void writeExcel(List<List<String>> dataList) {
            
            if(dataList != null) {
                
                Workbook wb = new HSSFWorkbook();
                //创建一个excel表
                Sheet sheet1 = wb.createSheet("sheet1");
                //创建第1行
                Row row1 = sheet1.createRow(0);
                //合并第一行1到7列
                CellRangeAddress mergeCell0 = new CellRangeAddress(0,0,0,6);
                
                sheet1.addMergedRegion(mergeCell0);
                //合并第二行1到7列
                CellRangeAddress mergeCell1 = new CellRangeAddress(1,1,0,6);
                
                sheet1.addMergedRegion(mergeCell1);
                //创建第1行的第1列
                Cell cell1Row1 = row1.createCell(0);
                //给第1行第1列的单元格赋值
                cell1Row1.setCellValue("押品报表");
                //创建单元格格式
                CellStyle cellStyle0 = wb.createCellStyle();
                //设置单元格格式居中
                cellStyle0.setAlignment(CellStyle.ALIGN_CENTER);
                //第1行1列居中
                cell1Row1.setCellStyle(cellStyle0);
                //创建第二行
                Row row2 = sheet1.createRow(1);
                //创建第二行一列
                Cell cell1Row2 = row2.createCell(0);
                
                SimpleDateFormat sf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
                //给第二行一列赋值
                cell1Row2.setCellValue("日期:"+sf.format(new Date()));
                //创建第三行
                Row row3 = sheet1.createRow(2);
                //第三行一列
                Cell cell1Row3 = row3.createCell(0);
                
                cell1Row3.setCellValue("一列");
                
                cell1Row3.setCellStyle(cellStyle0);
                //第三行二列
                Cell cell2Row3 = row3.createCell(1);
                
                cell2Row3.setCellValue("二列");
                
                cell2Row3.setCellStyle(cellStyle0);
                //第三行三列
                Cell cell3Row3 = row3.createCell(2);
                
                cell3Row3.setCellValue("三列");
                
                cell3Row3.setCellStyle(cellStyle0);
                //第三行四列
                Cell cell4Row3 = row3.createCell(3);
                
                cell4Row3.setCellValue("四列");
                
                cell4Row3.setCellStyle(cellStyle0);
                //第三行五列
                Cell cell5Row3 = row3.createCell(4);
                
                cell5Row3.setCellValue("五列");
                
                cell5Row3.setCellStyle(cellStyle0);
                //第三行六列
                Cell cell6Row3 = row3.createCell(5);
                
                cell6Row3.setCellValue("六列");
                
                cell6Row3.setCellStyle(cellStyle0);
                //第三行七列
                Cell cell7Row3 = row3.createCell(6);
                
                cell7Row3.setCellValue("七列");
                
                cell7Row3.setCellStyle(cellStyle0);
                //设置数据单元格的格式
                CellStyle cellStyleData = wb.createCellStyle();
                
                cellStyleData.setBorderBottom(CellStyle.BORDER_THIN);
                
                cellStyleData.setBorderLeft(CellStyle.BORDER_THIN);
                
                cellStyleData.setBorderRight(CellStyle.BORDER_THIN);
                
                cellStyleData.setBorderTop(CellStyle.BORDER_THIN);
                
                cellStyleData.setAlignment(CellStyle.ALIGN_CENTER);
                //录入数据
                for(int i=0;i<dataList.size();i++) {
                    
                    Row row = sheet1.createRow(i+3);
                    
                    Cell cell1 = row.createCell(0);
                        
                    cell1.setCellStyle(cellStyleData);
                        
                    cell1.setCellValue(dataList.get(i).get(0));
                        
                    Cell cell2 = row.createCell(1);
                        
                    cell2.setCellStyle(cellStyleData);
                    
                    cell2.setCellValue(dataList.get(i).get(1));
                        
                    Cell cell3 = row.createCell(2);
                        
                    cell3.setCellStyle(cellStyleData);
                    
                    cell3.setCellValue(dataList.get(i).get(2));
                        
                    Cell cell4 = row.createCell(3);
                        
                    cell4.setCellStyle(cellStyleData);
                    
                    cell4.setCellValue(dataList.get(i).get(3));
                        
                    Cell cell5 = row.createCell(4);
                        
                    cell5.setCellStyle(cellStyleData);
                    
                    cell5.setCellValue(dataList.get(i).get(4));
                        
                    Cell cell6 = row.createCell(5);
                        
                    cell6.setCellStyle(cellStyleData);
                    
                    cell6.setCellValue(dataList.get(i).get(5));
                        
                    Cell cell7 = row.createCell(6);
                        
                    cell7.setCellStyle(cellStyleData);
                    
                    cell7.setCellValue(dataList.get(i).get(6));
                }
                
                OutputStream out = null;
                
                try {
                    
                    out = new FileOutputStream("D:/export.xls");
                    
                    wb.write(out);
                } catch (Exception e) {
                    //
                    e.printStackTrace();
                }finally {
                    
                    try {
                        
                        out.close();
                    } catch (IOException e) {
                        //
                        e.printStackTrace();
                    }
                }
            }
        }
        
        public static void main(String[] args) {
            
              List<List<String>> list = new ArrayList<List<String>>();
              
              List<String> strList = new ArrayList<String>();
              
              strList.add("1");strList.add("2");strList.add("3");strList.add("4");
              
              strList.add("六");strList.add("七");strList.add("八");strList.add("九");
              
              list.add(strList);
              
              List<String> strList1 = new ArrayList<String>();
              
              strList1.add("6");strList1.add("7");strList1.add("8");strList1.add("9");
              
              strList1.add("十一");strList1.add("十二");strList1.add("十三");strList1.add("十四");
              
              list.add(strList1);
            
              ExportExcelFile.writeExcel(list);
        }
    }

    三、将数据输出到本地文件export.xls中,结果如下所示:

  • 相关阅读:
    模块和包专区
    递归函数,三级菜单专区
    内置函数和匿名函数专区
    迭代器和生成器专区
    函数进阶专区
    初始函数专区
    题解 P6282 【[USACO20OPEN]Cereal S】
    题解 P6281 【[USACO20OPEN]Social Distancing S】
    题解 P6281 【[USACO20OPEN]Social Distancing S】
    第5题:棋盘
  • 原文地址:https://www.cnblogs.com/zzb-yp/p/9849477.html
Copyright © 2011-2022 走看看