8535.com-新浦京娱乐场官网|欢迎您

新浦京娱乐场官网Java创立Excel-DEMO,javaexcel-demo

来源:http://www.dnamique.com 作者:计算机网络 人气:66 发布时间:2019-10-07
摘要:Java创建Excel-DEMO,javaexcel-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.Da

Java创建Excel-DEMO,javaexcel-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流。
    }
}

 

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateForm...

本文由8535.com-新浦京娱乐场官网|欢迎您发布于计算机网络,转载请注明出处:新浦京娱乐场官网Java创立Excel-DEMO,javaexcel-demo

关键词:

上一篇:没有了

下一篇:没有了

频道精选

最火资讯