<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    一個生成excel的工具類

    很多人都直接編輯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("http://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();
            Element root = (Element) ((Element) temp.get(ROOT)).clone();
            Document document = DocumentHelper.createDocument();
            document.setRootElement(root);
            Element tableEle = root.element("table");
            tableEle.add(parseTitleElement(titleObj));
            for (int i = 0; i < list.size(); i++) {
                tableEle.add(parseRecordElement(list.get(i)));
            }
            try {
                OutputFormat format = new OutputFormat("", true, "GB2312");
                XMLWriter writer = new XMLWriter(os, format);
                writer.write(document);
                writer.flush();
                writer.close();
                os.close();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                throw new RuntimeException("Parse outstream error, Cause:", e);
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException("Parse outstream error, Cause:", e);
            }
            return os;
        }

        public static Element parseTitleElement(Object titleObj)
        {
            Element titleEle = (Element) ((Element) temp.get(TITLE)).clone();
            if (null == titleObj) return titleEle;
            List tdList = titleEle.selectNodes("td");
            Element td;
            for (int i = 0; i < tdList.size(); i++) {
                td = (Element) tdList.get(i);
                fullField(td, titleObj);
            }
            return titleEle;
        }

        public static Element parseRecordElement(Object recordObj)
        {
            Element recordEle = (Element) ((Element) temp.get(RECORD)).clone();
            List tdList = recordEle.selectNodes("td");
            Element td;
            for (int i = 0; i < tdList.size(); i++) {
                td = (Element) tdList.get(i);
                fullField(td, recordObj);
            }
            return recordEle;
        }

        public static void fullField(Element tdEle, Object obj)
        {
            Attribute att = tdEle.attribute("id");
            if (null == att || null == att.getText() || 0 == att.getText().trim().length()) {
                return;
            }
            String fieldName = att.getText();
            if (null == fieldName || fieldName.trim().length() == 0) return;
            Method[] objMethod = obj.getClass().getDeclaredMethods();

            Object value;
            for (int i = 0; i < objMethod.length; i++) {
                if (("get" + (fieldName.trim())).equals(objMethod[i].getName())) {
                    try {
                        value = objMethod[i].invoke(obj, new Object[]{});
                        value = (null == value ? "" : value);
                        tdEle.setText(value.toString());
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                        throw new RuntimeException(e);
                    }
                }
            }
        }

        public static String removeXMLHeader(OutputStream os)
        {
            String xml = os.toString();
            int position = xml.indexOf(">");
    //        xml = xml.substring(position+1,xml.length());
    //        position = xml.indexOf(">");
            return xml.substring(position + 1, xml.length());
        }
    }

    調用
    OutputStream os = response.getOutputStream();
                response.setHeader(
    "Content-disposition""attachment; filename=" + excelFileName);
                response.setContentType(
    "application/msexcel");
                File excelTemplateFile 
    = new File(report_path + templateFileName);
                String out 
    = ExcelTemplateUtil.generateListToTemplate(titleObj, resultList, excelTemplateFile);
                response.getOutputStream().write(out.getBytes());
                os.close();

    posted on 2007-08-08 21:47 wanglin 閱讀(1348) 評論(3)  編輯  收藏

    評論

    # re: 一個生成excel的工具類 2007-08-09 15:20 靜兒

    原來excel報表通過html自動生成啊。  回復  更多評論   

    # re: 一個生成excel的工具類[未登錄] 2007-08-09 17:43 -274°C

    <?php
    header("Content-Type:application/vnd.ms-execl");
    header("Content-Disposition:filename=test.xls");
    ……
    很方便,其他語言道理一樣。  回復  更多評論   

    # re: 一個生成excel的工具類 2008-01-28 09:02 wanglin

    有更好的方法
    xlsWin = window.open('',''_blank, openPara);
    xlsWin.document.write(inStr);
    xlsWin.document.close();
    xlsWin.document.execCommand('Saveas', true, fileName);

    將response流instr放到一個document文件中保存。
      回復  更多評論   


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2008年1月>
    303112345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲五月丁香综合视频| 久久亚洲精品国产精品黑人| 亚洲专区中文字幕| 久久精品一本到99热免费| 亚洲人成电影福利在线播放| 久久精品免费电影| 99久久亚洲综合精品成人网| 91成人在线免费视频| 亚洲AV无码专区在线亚| 永久黄网站色视频免费| 国产成人综合亚洲一区| 国产日韩成人亚洲丁香婷婷| aaa毛片视频免费观看| 亚洲国产精品无码专区| 99久久久国产精品免费牛牛 | 亚洲色欲色欲www在线丝| 大地资源中文在线观看免费版| 亚洲成a人片在线观看无码专区| 日本在线免费播放| 亚洲Av高清一区二区三区| 处破痛哭A√18成年片免费| 国产成人亚洲精品无码AV大片| 亚洲性日韩精品一区二区三区| jizz免费在线观看| 久久亚洲精品成人无码网站| 中国在线观看免费高清完整版 | 亚洲精品成人片在线观看| 在线免费观看伊人三级电影| 亚洲天堂在线播放| 韩国二级毛片免费播放| 中文字幕av免费专区| 亚洲午夜精品一区二区公牛电影院| 日本一道一区二区免费看| 中文字幕免费人成乱码中国| 亚洲国产高清视频在线观看| 国产18禁黄网站免费观看| 今天免费中文字幕视频| 亚洲欧美熟妇综合久久久久| 亚洲一级片内射网站在线观看| 91福利免费视频| www免费插插视频|