java 產生簡單word(利用Itext工具),產生簡單Excel,以及下載筆記

來源:互聯網
上載者:User

標籤:ati   link   參考   eth   demo   encoding   紅色   sts   div   

1.java 產生簡單word(包含圖片表格)

pom中加入itext 相關依賴

<dependency>       <groupId>com.lowagie</groupId>        <artifactId>itext</artifactId>       <version>2.1.7</version></dependency>         <dependency>        <groupId>com.lowagie</groupId>        <artifactId>itext-rtf</artifactId>        <version>2.1.7</version></dependency>

2.java相關代碼(資料與圖片可以忽略)

/**     * 建立報告(報告名字。圖片。新聞id)     *      * @throws JSONException     */    @Override    public String addReport(String[] img, String[] newsId, String reportName, String loginId, String path,            String imgPath) {        Long name = System.currentTimeMillis();        String filePath = path + File.separator + name + ".doc";        File file = new File(filePath);        Report report = new Report();        report.setLoginId(loginId);        report.setReportName(reportName);        report.setDownUrl(filePath);        fileDao.addReport(report);        System.out.println("插入報告成功" + report.getReportNo());             Document document = new Document(PageSize.A4);        try {            RtfWriter2.getInstance(document, new FileOutputStream(file));        } catch (FileNotFoundException e2) {            // TODO Auto-generated catch block            e2.printStackTrace();        }        document.open();        BaseFont bfChinese = null;        try {            bfChinese = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);        } catch (DocumentException | IOException e1) {            e1.printStackTrace();        }        Font titleFont = new Font(bfChinese, 22, Font.BOLD);        Font contextFont = new Font(bfChinese, 10, Font.NORMAL);        Paragraph title = new Paragraph("統計報告", titleFont);        title.setAlignment(Element.ALIGN_CENTER);        // 紅色的線        RtfShapePosition position;        position = new RtfShapePosition(150, 0, 10400, 170);        position.setXRelativePos(RtfShapePosition.POSITION_X_RELATIVE_MARGIN);        position.setYRelativePos(RtfShapePosition.POSITION_Y_RELATIVE_PARAGRAPH);        RtfShape shape = new RtfShape(RtfShape.SHAPE_RECTANGLE, position);        RtfShapeProperty property = new RtfShapeProperty(RtfShapeProperty.PROPERTY_LINE_COLOR, Color.RED);        shape.setProperty(property);        Paragraph p1 = new Paragraph(shape);        try {            // logo            Image imglogo = Image.getInstance(imgPath + File.separator + "dfgx.jpg");            imglogo.setAlignment(Image.LEFT);// 設定圖片顯示位置            imglogo.scaleAbsolute(80, 50);// 直接設定顯示尺寸            document.add(imglogo);            document.add(title);            document.add(p1);        } catch (Exception e) {            e.printStackTrace();        }        Table table = null;        try {            table = new Table(4);            int width[] = { 40, 30, 15, 15 };// 設定每列寬度比例            table.setWidths(width);        } catch (Exception e1) {            e1.printStackTrace();        }        table.setBorderWidth(1);        table.setBorderColor(Color.BLACK);        table.setPadding(0);        table.setSpacing(0);        try {            table.addCell("標題");            table.addCell("連結");            table.addCell("渠道");            table.addCell("時間");        } catch (BadElementException e1) {            e1.printStackTrace();        }        List<Object> list = estoWeb.SelectId(newsId);        String time = "";        String titleinfo = "";        String url = "";        String channel = "";        try {            for (Object string : list) {                JSONObject json = null;                json = new JSONObject(String.valueOf(string));                channel = (String) json.get("webCell");                titleinfo = (String) json.get("titleCell");                url = (String) json.get("linkCell");                time = (String) json.get("dateCell");                table.addCell(new Paragraph(titleinfo));                table.addCell(new Paragraph(url));                table.addCell(new Paragraph(channel));                table.addCell(new Paragraph(time));            }        } catch (Exception e) {            e.printStackTrace();        }        try {            document.add(table);        } catch (DocumentException e1) {            e1.printStackTrace();        }        for (String string : img) {            Map<String, Object> map = fileDao.findReportPath(string);            System.out.println(map.get("PIC_PATH"));            Image img1 = null;            try {                img1 = Image.getInstance(imgPath + File.separator + String.valueOf(map.get("PIC_PATH")));            } catch (BadElementException e) {                e.printStackTrace();            } catch (MalformedURLException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            }            img1.setAbsolutePosition(0, 0);            img1.setAlignment(Image.ALIGN_CENTER);// 設定圖片顯示位置            img1.scaleAbsolute(360, 180);// 直接設定顯示尺寸            try {                document.add(img1);            } catch (DocumentException e) {                e.printStackTrace();            }        }        document.close();        return "ok";    }

3.產生簡單Excel(參考 :http://blog.csdn.net/yongqingmiao/article/details/7179452)

pom加入相關依賴

<dependency>            <groupId>jexcelapi</groupId>            <artifactId>jxl</artifactId>            <version>2.6</version></dependency>        

4.java代碼

File file;        WritableWorkbook wwb;        try {            file = new File(imgpath + File.separator + "news.xls");            wwb = Workbook.createWorkbook(file);            WritableSheet ws = wwb.createSheet("頁1", 0);            ws.addCell(new Label(0, 0, "時間"));            ws.addCell(new Label(1, 0, "標題"));            ws.addCell(new Label(2, 0, "連結"));            ws.addCell(new Label(3, 0, "渠道"));            List<Object> list = estoWeb.SelectId(newsId);            String time = "";            String titleinfo = "";            String url = "";            String channel = "";            try {                for (int i = 0; i < list.size(); i++) {                    JSONObject json = null;                    json = new JSONObject(String.valueOf(list.get(i)));                    channel = (String) json.get("webCell");                    titleinfo = (String) json.get("titleCell");                    url = (String) json.get("linkCell");                    time = (String) json.get("dateCell");                                        ws.addCell(new Label(0, i + 1, time));                    ws.addCell(new Label(1, i + 1, titleinfo));                    ws.addCell(new Label(2, i + 1, url));                    ws.addCell(new Label(3, i + 1, channel));                                    }                wwb.write();                wwb.close();            } catch (Exception e) {                e.printStackTrace();            }        } catch (Exception e) {            e.printStackTrace();        }

5.下載demo(相關檔案名稱參數參考)

InputStream inputStream = null;        OutputStream os = null;        res.setCharacterEncoding("utf-8");        res.setContentType("multipart/form-data");        res.setHeader("Content-Disposition", "attachment;filename=\"" + "news.xls" + "\"");        try {            // 下載檔案            inputStream = new FileInputStream(new File(imgpath + File.separator + "news.xls"));            os = res.getOutputStream();            byte[] b = new byte[4096];            int length;            while ((length = inputStream.read(b)) > 0) {                os.write(b, 0, length);            }        } catch (Exception e) {            e.printStackTrace();            return "false";        } finally {            try {                os.flush();                os.close();                inputStream.close();                if (new File(imgpath + File.separator + "news.xls").exists()) {                    new File(imgpath + File.separator + "news.xls").delete();                }            } catch (Exception e) {                e.printStackTrace();                return "false";            }        }

 

java 產生簡單word(利用Itext工具),產生簡單Excel,以及下載筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.