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流。
    }
}
原文地址:https://www.cnblogs.com/smart-hwt/p/8243577.html