zoukankan      html  css  js  c++  java
  • 导出Excel

    package com.excel.export;
    
    import java.io.File;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.commons.io.FileUtils;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    public class ExportExcel2 {
    
        public static void main(String[] args) throws Exception {
            List<String> title = new ArrayList<String>();
            title.add("id");
            title.add("name");
            title.add("sex");
            List<List<String>> contents = new ArrayList<List<String>>();
            for(int i=1;i<11;i++){
                List<String> list = new ArrayList<String>();
                list.add(i+"");
                list.add("James_"+i);
                list.add("man");
                contents.add(list);
            }
            
            if (title == null) {
                throw new Exception("导出表头不能为空");
            }
            if (contents != null && contents.get(0).size() != title.size()) {
                throw new Exception("表头和行数据不对称");
            }
            
            //创建Excel工作簿
            XSSFWorkbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet();
            Row row = sheet.createRow(0);
            Cell cell = null;
            //插入第一行数据 id,name,sex
            for(int i=0;i<title.size();i++){
                cell = row.createCell(i);
                cell.setCellValue(title.get(i));
            }
            //追加数据
            
            for(int i=1;i<=contents.size();i++){
                Row nextRow = sheet.createRow(i);
                for(int j=0;j<contents.get(i-1).size();j++){
                    Cell cell2 = nextRow.createCell(j);
                    cell2.setCellValue(contents.get(i-1).get(j));
                }
            }
            //创建一个文件
            File file = new File("e:/content.xlsx");
            try {
                file.createNewFile();
                FileOutputStream fop = FileUtils.openOutputStream(file);
                workbook.write(fop);
                fop.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("------done----");
            
        }
    
    }

     http://blog.csdn.net/ptzrbin/article/details/8751293

  • 相关阅读:
    .htaccess注释
    Ubuntu开机自启动jar包和Nginx
    Rook部署和管理Ceph集群
    Python 打包 Nuitka
    Python 反射 备查
    Python 屏幕坐标点取色
    Python pynput 监听事件
    【线性代数】基本概念
    C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)
    Asp.Net Core Swagger 接口分组(支持接口一对多暴露)
  • 原文地址:https://www.cnblogs.com/james-roger/p/5394215.html
Copyright © 2011-2022 走看看