java生成excel文档

要做一个后台自动化,要先预先生成一份文档,以下内容生成了文档

首先下载jxl.jar包,下载地址:http://download.csdn.net/detail/prstaxy/4469935

1.生成一个简单的excel文档

WritableWorkbook workbook = null;
        try {
            workbook = Workbook.createWorkbook(new File("F:\\test.xls"));
            //创建新的一页
            WritableSheet sheet = workbook.createSheet("First Sheet", 0);
            //创建要显示的具体内容
            Label formate = new Label(0,0,"数据格式");
            sheet.addCell(formate);
            Label floats = new Label(1,0,"浮点型");
            sheet.addCell(floats);
            Label integers = new Label(2,0,"整型");
            sheet.addCell(integers);
            Label booleans = new Label(3,0,"布尔型");
            sheet.addCell(booleans);
            Label dates = new Label(4,0,"日期格式");
            sheet.addCell(dates);

            Label example = new Label(0,1,"数据示例");
            sheet.addCell(example);
            //浮点数据
            Number number = new Number(1,1,3.1415926535);
            sheet.addCell(number);
            //整形数据
            Number ints = new Number(2,1,15042699);
            sheet.addCell(ints);
            Boolean bools = new Boolean(3,1,true);
            sheet.addCell(bools);
            //日期型数据
            Calendar c = Calendar.getInstance();
            Date date = c.getTime();
            WritableCellFormat cf1 = new WritableCellFormat(DateFormats.FORMAT1);
            DateTime dt = new DateTime(4,1,date,cf1);
            sheet.addCell(dt);
            //把创建的内容写入到输出流中,并关闭输出流

        } catch (RowsExceededException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (WriteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        workbook.write();
        System.out.println("写入成功");
        workbook.close(); */

生成的内容为

2.写入大数据,在指定的列数据中生成一个随机数

public String  createOrderFile() throws RowsExceededException, WriteException {

        // 生成随机单号
        randomePlatformID= getRandomNumber(11);
        randomePlatformID = "PM" + randomePlatformID;

File file = new File(""); //获取项目的绝对地址
         String abspath = file.getAbsolutePath();

        createUrl= abspath + "\\OfflineOrders.xls";
        Log.logInfo("地址是:" + createUrl);
        WritableWorkbook workbook = null ;
        WritableSheet sheet = null;
        try {
            workbook = Workbook.createWorkbook(new File(createUrl));
            sheet = workbook.createSheet("First Sheet", 0);
        } catch (IOException e) {
            e.printStackTrace();
            Log.logInfo("创建文件失败");
        }
        String[] cellText = { "平台", "所属人", "对方店铺账号", "付款ID", "平台单号", "订单运费", "发货仓库", "发货渠道", "收件人国家简码"
                , "公司名称", "收件人姓名","州", "城市", "地址1", "地址2", "邮编", "电话", "Email", "SKU1", "数量1", "单价", "币种" };
        String[] contentText = { "JOOM", "JM3901556", "", "", "PM789", "", "SZ", "DEDHL", "DE", "", "Daniela Weihrauch"
                ,"Berlin", "Berlin", "Stellingdamm 9", "", "55555", "+49 3068838320", "", "109036701", "3", "17","USD" };
        // 生成列头
        for (int i = 0; i < cellText.length; i++) {
            Label columnHeader = new Label(i, 0, cellText[i]);
            try {
                sheet.addCell(columnHeader);
            } catch (RowsExceededException e) {
                e.printStackTrace();
            } catch (WriteException e) {
                e.printStackTrace();
            }
        }
                // 填入正文内容
                for (int j = 0; j < contentText.length; j++) {
                    Label columnBody;
                    if (j == 4) {// 平台订单号单独处理
                        Log.logInfo("生成随机10位数平台订单编号为:" + randomePlatformID);
                        columnBody = new Label(j, 1, randomePlatformID);
                    } else {
                        columnBody = new Label(j, 1, contentText[j]);
                    }
                    sheet.addCell(columnBody);
                }
                try {
                    workbook.write();
                    Log.logInfo("写入成功");
                    try {
                        workbook.close();
                    } catch (WriteException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

        return randomePlatformID;

    }
    

时间: 2024-08-24 13:10:25

java生成excel文档的相关文章

Apache POI -- Java 导出Excel文档(笔记)

一.Action类 import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Met

php用PHPExcel库生成Excel文档的例子

<?php require_once '../libs/PHPWord/PHPWord.php'; require_once '../libs/PHPWord/PHPWord/IOFactory.php'; require_once '../../config.php'; // New Word Document $PHPWord = new PHPWord(); $objExcel->getProperties()->setCreator("office 2003 excel

后台生成EXCEL文档,自定义列

后台生成EXCEL文档,自定义列 //response输出流处理 //设置编码.类型.文件名 getResponse().reset(); getResponse().setCharacterEncoding("utf-8"); getResponse().setContentType("application/vnd.ms-excel"); String useragent = getRequest().getHeader("user-agent&quo

[转载]Java生成Word文档

在开发文档系统或办公系统的过程中,有时候我们需要导出word文档.在网上发现了一个用PageOffice生成word文件的功能,就将这块拿出来和大家分享. 生成word文件与我们编辑word文档本质上是相同的,只不过在用程序生成word文档的时候换成了用代码来操作.下面的例子中主要有添加标题,正文(段落.字体.字号.倍行间距.对齐方式.首行缩进等),插入图片等操作.这里写的例子给出的内容只是PageOffice 组件生成word文件的部分方法,需要使用更多方法的还是要自己根据实际的需求去查看AP

Java生成word文档

itext-rtf-2.1.7.jar,下载地址:http://download.csdn.net/detail/xuxu198899223/7717727 itext-2.1.7.jar 下载地址:http://download.csdn.net/detail/xuxu198899223/7717717 package word; import java.io.FileOutputStream; import java.io.IOException; import java.util.Arra

Java生成PDF文档(表格、列表、添加图片等)

需要的两个包及下载地址: (1)iText.jar:http://download.csdn.net/source/296416 (2)iTextAsian.jar(用来进行中文的转换):http://download.csdn.net/source/172399 代码如下: 1 import java.awt.Color; 2 import java.io.FileOutputStream; 3 import com.lowagie.text.Cell; 4 import com.lowagi

java生成word文档【二】

/** * <p>expWordByItext方法主要用于-采用itext导出文档到word.</p> * <p>优缺点-可将随意控制生成word的格式,但代码控制上比采用模板的方式稍微复杂,可设置页眉页脚等其他文档属性.</p> * <p>第三方jar-itext-2.1.7.jar.itext-rtf-2.1.7.jar.<br> * import com.lowagie.text.Document;<br> * im

JAVA生成Word文档(经过测试)

首先告诉大家这篇文章的原始出处:http://www.havenliu.com/java/514.html/comment-page-1#comment-756 我也是根据他所描述完成的,但是有一些地方有点模糊,不容易弄出来.所以,我另外写一篇,让大家少走一些弯路. 上图:是Word文档中的内容,也就是模板,为了下面步鄹做铺垫,所以在需要输入数据的地方改成了拼音, 将word文档另存为xml文件. 接下来,上面写的拼音就起到作用了. 打开xml文件.搜索 title. 将Title 改为 ${t

java生成PDF文档

依赖jar包:iText-2.1.7.jar.iTextAsian.jar.itext-rtf-2.1.7.jar 代码如下: package com.servlet; import java.awt.Color; import java.io.FileOutputStream; import java.text.DecimalFormat; import java.util.Arrays; import java.util.HashSet; import java.util.List; imp