RSS
热门关键字:  java  Ajax  JSP  JSF  Struts
当前位置 : 首页>Java>列表

一个生成excel的工具类

来源: 作者: 时间:2007-08-14 点击:
很多人都直接编辑html,保存成xls就当成excel报表了。挺方便的,于是我写了这个简化工作的工具类——将一个html的表格模板解析成一个xls报表
模板如下
<?xml version="1.0" encoding="GB2312" ?>
<div style="width:100%;height:450;overflow-x:auto;overflow-y:auto">
    <table width="100%" border="1" cellspacing="2" cellpadding="0">
            <tr id="title" bgcolor="#fefcce">
                <td nowrap="true" >客户</td>
                <td nowrap="true" >产品</td>
                <td nowrap="true" >中文名称</td>
                <td nowrap="true" >英文名称</td>
                <td nowrap="true" >产品分类</td>
                <td nowrap="true" >包装</td>
                <td nowrap="true" >单位</td>
                <td nowrap="true" >数量</td>
                <td nowrap="true" >冻结数量</td>
                <td nowrap="true" >可用数量</td>
                <td nowrap="true"  id="CUBIC"></td>
                <td nowrap="true"  id="WEIGHT"></td>
            </tr>
            <tr id="record">
                <td nowrap="true" id="CUSTOMERID"></td>
                <td nowrap="true" id="SKU_ID"></td>
                <td nowrap="true" id="SKU_DESCR_C"></td>
                <td nowrap="true" id="SKU_DESCR_E"></td>
                <td nowrap="true" id="SKU_CLASS"></td>
                <td nowrap="true" id="PACKAGE_ID"></td>
                <td nowrap="true" id="UOM"></td>
                <td nowrap="true" id="QUANTITY"></td>
                <td nowrap="true" id="FREEZE_QUANTITY"></td>
                <td nowrap="true" id="AVAILABLE_QUANTITY"></td>
                <td nowrap="true" id="CUBIC"></td>
                <td nowrap="true" id="WEIGHT"></td>
            </tr>
    </table>
</div>
工具类如下

public class ExcelTemplateUtil {
    private static String CHARSET = "";
    private static final String ROOT = "ROOT";
    private static final String TITLE = "TITLE";
    private static final String RECORD = "RECORD";
    private static Map temp = new HashMap();

    public static String generateListToTemplate(Object titleObj, List recordList, File templateFile)
    {
        readTemplateFile(templateFile);
        ByteArrayOutputStream os = (ByteArrayOutputStream) builderExcelOutput(titleObj, recordList);
        return removeXMLHeader(os);
    }

    public static void readTemplateFile(File file)
    {
        try {
            Document templateDocument = new SAXReader().read(file);
            Element root = templateDocument.getRootElement();
            List trList = root.selectNodes("//div/table/tr");
            Element titleTemp = (Element) trList.get(0);
            Element recordTemp = (Element) trList.get(1);
            root.element("table").remove(titleTemp);
            root.element("table").remove(recordTemp);
            temp.put(TITLE, trList.get(0));
            temp.put(RECORD, trList.get(1));
            temp.put(ROOT, root);
        } catch (DocumentException e) {
            e.printStackTrace();
            throw new RuntimeException("Parse xml file error, Cause:", e);
        }
    }

    public static OutputStream builderExcelOutput(Object titleObj, List list)
    {

        ByteArrayOutputStream os = new ByteArrayOutputStream();

共3页: 上一页 1 [2] [3] 下一页
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册