zoukankan      html  css  js  c++  java
  • Java创建Excel-DEMO

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import jxl.Workbook;
    import jxl.write.DateFormats;
    import jxl.write.DateTime;
    import jxl.write.Label;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    
    public class SimpleExcelDemo {
        private static int column = 0;
        private static int row = 0;
        //创建工作薄
        public static void createExcelFile(String demand_id) throws WriteException,IOException{
            //创建文件目录
            String realPath = "C:\excel\";
            File fileDir = new File(realPath);
            if(!fileDir.exists()){
                fileDir.mkdirs();
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(new Date(System.currentTimeMillis()));
            String fileName = date + ".xls";
            String filePath = realPath + fileName;
            File file = new File(filePath);
            WritableCellFormat wf = new WritableCellFormat(DateFormats.FORMAT1);
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String current = sdf1.format(new Date(System.currentTimeMillis()));
            // 创建一个文件输出流,用于写出表格到本地文件夹
            OutputStream out = null;
            WritableWorkbook wwb = null;
            WritableSheet sheet = null;
            Label cell;
            try {
                if(!file.exists()){
                    out = new FileOutputStream(file);
                    //创建excel文件(创建一个可读写入的工作薄)
                    wwb = Workbook.createWorkbook(out);
                    //创建新的一页
                    sheet = wwb.createSheet("已处理", 0);
                    //创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
                    cell = new Label(column, row, "需求ID");
                    sheet.addCell(cell);
                    cell = new Label(column + 1, row, "上传时间");
                    sheet.addCell(cell);
                    cell = new Label(column, ++row, demand_id);
                    sheet.addCell(cell);
                    Date d = new Date();
                    d = sdf1.parse(current);
                    DateTime dt = new DateTime(column + 1, row, d, wf);
                    sheet.addCell(dt);
                }else{
                    File[] files = fileDir.listFiles();
                    for(File f:files){
                        if(f.getName().equals(fileName)){
                            Workbook wb = Workbook.getWorkbook(f);
                            wwb = Workbook.createWorkbook(new File(realPath + fileName), wb); 
                            sheet = wwb.getSheet("已处理");
                            cell = new Label(column, ++row, demand_id);
                            sheet.addCell(cell);
                            Date d = new Date();
                            d = sdf1.parse(current);
                            DateTime dt = new DateTime(column + 1, row, d, wf);
                            sheet.addCell(dt);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            wwb.write(); // 写入Excel工作表
            wwb.close(); // 关闭Excel工作表,同时也会关闭IO流。
        }
        
        public static void main(String[] args) throws Exception {
            createExcelFile("1234");
            System.out.println(column + "," + row);
            //创建文件目录
            File fileDir = new File("C:\excel");
            if(!fileDir.exists()){
                fileDir.mkdirs();
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(new Date(System.currentTimeMillis()));
            String fileName = date + ".xls";
            String filePath = "C:\excel\" + fileName;
            File file = new File(filePath);
            // 创建一个文件输出流,用于写出表格到本地文件夹
            OutputStream out = new FileOutputStream(file);
            //创建excel文件(创建一个可读写入的工作薄)
            WritableWorkbook wwb = Workbook.createWorkbook(out);
            //创建新的一页
            WritableSheet sheet = wwb.createSheet("已处理", 0);
            //创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
            Label cell = new Label(0,0,"需求ID");
            sheet.addCell(cell);
            cell = new Label(1,0,"上传时间");
            sheet.addCell(cell);
            cell = new Label(0, 1, "5055");
            sheet.addCell(cell);
            WritableCellFormat  wf = new WritableCellFormat(DateFormats.FORMAT1);
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String current = sdf1.format(new Date(System.currentTimeMillis()));
            Date d = sdf1.parse(current);
            DateTime birthday = new DateTime(1, 1, d, wf);
            sheet.addCell(birthday);
            wwb.write(); // 写入Excel工作表
            wwb.close(); // 关闭Excel工作表,同时也会关闭IO流。
        }
    }
  • 相关阅读:
    .NET Core WEB API使用Swagger生成在线接口文档
    .NET Core WEB API中接口参数的模型绑定的理解
    .Net Core使用视图组件(ViewComponent)封装表单文本框控件
    在有主分支和个人分支情况下的TFS使用方法
    SQL Server Profiler (SQl跟踪器)的简单使用
    C# 多线程下的单例模式
    C# 单例模式
    .net core MVC接受来自自前端的GET和POST请求方法的区别
    《Windows Phone 8 Development Internals》读书笔记-1-2-2-连载
    《Windows Phone 8 Development Internals》读书笔记-1-2-1-连载
  • 原文地址:https://www.cnblogs.com/smart-hwt/p/8243577.html
Copyright © 2011-2022 走看看