Java:Excel轉PDF實現方案;基於POI與Itext進行搭配.

做者博客地址更換至CSDN,地址:http://blog.csdn.net/littlebrain4solvingjava

 

說明:apache

1.最近業務需求涉及到了關於這方面的知識,在網上尋找了不少次都是一些零零碎碎的代碼,如今歸檔記錄下來以避免之後忘記再回來翻閱一下;同時讓有需求的朋友也能夠借鑑一下,若是有興趣的朋友能夠本身寫一套新方案出來,順便發我一個連接我,這樣共同窗習哈。api

2.此種方式能夠實現多個Excel轉PDF多頁狀況,對邊框和一些精細的部分實現的可能不太好,因此有能力的朋友本身能夠參考如下代碼進行適量的修改.dom

3.原理:使用PDF的Table與Excel表格進行對應,並解析Excel的行、列、單元格樣式與Table的進行匹配.(樣式有些部分實現的不是太好,全部你們有問題多本身解決一下啊)xss

 

進入正題,首先下載所需的Jar包:ide

commons-codec.jar
commons-io-1.1.jar
dom4j-1.6.1.jar
poi-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar
stax-api-1.0.1.jar
xmlbeans-2.3.0.jar
itext-asian.jar (亞洲語言支持包)
itextpdf-5.4.4.jar
xmlworker-5.4.4.jar (支持Html轉PDF,可選..)學習

(POI官方網站) http://poi.apache.org/字體

(IText官方網站) http://www.itextpdf.com/網站

注:我採用的是 itext5.4.4 與 poi3.9.this

 

(*)效果圖以下:

 

(*)關聯代碼以下:

 1 class Excel {
 2 
 3     protected Workbook wb;
 4     protected Sheet sheet;
 5 
 6     public Excel(InputStream is) {
 7         try {
 8             this.wb = WorkbookFactory.create(is);
 9             this.sheet = wb.getSheetAt(wb.getActiveSheetIndex());
10         } catch (FileNotFoundException e) {
11             e.printStackTrace();
12         } catch (IOException e) {
13             e.printStackTrace();
14         } catch (InvalidFormatException e) {
15             e.printStackTrace();
16         }
17     }
18 
19     public Sheet getSheet() {
20         return sheet;
21     }
22     
23     public Workbook getWorkbook(){
24         return wb;
25     }
26 }
Excel
 1 public class ExcelObject {
 2     /**
 3      * 錨名稱
 4      */
 5     private String anchorName;
 6     /**
 7      * Excel Stream
 8      */
 9     private InputStream inputStream;
10     /**
11      * POI Excel
12      */
13     private Excel excel;
14     
15     public ExcelObject(InputStream inputStream){
16         this.inputStream = inputStream;
17         this.excel = new Excel(this.inputStream);
18     }
19 
20     public ExcelObject(String anchorName , InputStream inputStream){
21         this.anchorName = anchorName;
22         this.inputStream = inputStream;
23         this.excel = new Excel(this.inputStream);
24     }
25     public String getAnchorName() {
26         return anchorName;
27     }
28     public void setAnchorName(String anchorName) {
29         this.anchorName = anchorName;
30     }
31     public InputStream getInputStream() {
32         return this.inputStream;
33     }
34     public void setInputStream(InputStream inputStream) {
35         this.inputStream = inputStream;
36     }
37     Excel getExcel() {
38         return excel;
39     }
40 }
ExcelObject
 1 public class PdfTool {
 2     //
 3     protected Document document;
 4     //
 5     protected OutputStream os;
 6     
 7     public Document getDocument() {
 8         if (document == null) {
 9             document = new Document();
10         }
11         return document;
12     }
13 }
PdfTool
  1 public class Excel2Pdf extends PdfTool{
  2     //
  3     protected List<ExcelObject> objects = new ArrayList<ExcelObject>();
  4     
  5     /**
  6      * <p>Description: 導出單項PDF,不包含目錄</p>
  7      * @param object
  8      */
  9     public Excel2Pdf(ExcelObject object , OutputStream os) {
 10         this.objects.add(object);
 11         this.os = os;
 12     }
 13     
 14     /**
 15      * <p>Description: 導出多項PDF,包含目錄</p>
 16      * @param objects
 17      */
 18     public Excel2Pdf(List<ExcelObject> objects , OutputStream os) {
 19         this.objects = objects;
 20         this.os = os;
 21     }
 22     
 23     /**
 24      * <p>Description: 轉換調用</p>
 25      * @throws DocumentException
 26      * @throws MalformedURLException
 27      * @throws IOException
 28      */
 29     public void convert() throws DocumentException, MalformedURLException, IOException {
 30         getDocument().setPageSize(PageSize.A4.rotate());
 31         PdfWriter writer = PdfWriter.getInstance(getDocument(), os);
 32         writer.setPageEvent(new PDFPageEvent());
 33         //Open document
 34         getDocument().open();
 35         //Single one 
 36         if(this.objects.size() <= 1){
 37             PdfPTable table = this.toCreatePdfTable(this.objects.get(0) ,  getDocument() , writer);
 38             getDocument().add(table);
 39         }
 40         //Multiple ones 
 41         if(this.objects.size() > 1){
 42             toCreateContentIndexes(writer , this.getDocument() , this.objects);
 43             //
 44             for (int i = 0; i < this.objects.size(); i++) {
 45                 PdfPTable table = this.toCreatePdfTable(this.objects.get(i) , getDocument() , writer);
 46                 getDocument().add(table);
 47             }
 48         }
 49         //
 50         getDocument().close();
 51     }
 52     
 53     protected PdfPTable toCreatePdfTable(ExcelObject object , Document document , PdfWriter writer) throws MalformedURLException, IOException, DocumentException{
 54         PdfPTable table = new PdfTableExcel(object).getTable();
 55         table.setKeepTogether(true);
 56 //      table.setWidthPercentage(new float[]{100} , writer.getPageSize());
 57         table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
 58         return table;
 59     }
 60     
 61     /**
 62      * <p>Description: 內容索引建立</p>
 63      * @throws DocumentException 
 64      */
 65     protected void toCreateContentIndexes(PdfWriter writer , Document document , List<ExcelObject> objects) throws DocumentException{
 66         PdfPTable table = new PdfPTable(1);
 67         table.setKeepTogether(true);
 68         table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
 69         //
 70         Font font = new Font(Resource.BASE_FONT_CHINESE , 12 , Font.NORMAL);
 71         font.setColor(new BaseColor(0,0,255));
 72         //
 73         for (int i = 0; i < objects.size(); i++) {
 74             ExcelObject o = objects.get(i);
 75             String text = o.getAnchorName();
 76             Anchor anchor = new Anchor(text , font);
 77             anchor.setReference("#" + o.getAnchorName());
 78             //
 79             PdfPCell cell = new PdfPCell(anchor);
 80             cell.setBorder(0);
 81             //
 82             table.addCell(cell);
 83         }
 84         //
 85         document.add(table);
 86     }
 87     
 88     /**
 89      * <p>ClassName: PDFPageEvent</p>
 90      * <p>Description: 事件 -> 頁碼控制</p>
 91      * <p>Author: Cary</p>
 92      * <p>Date: Oct 25, 2013</p>
 93      */
 94     private static class PDFPageEvent extends PdfPageEventHelper{
 95         protected PdfTemplate template;
 96         public BaseFont baseFont;
 97         
 98         @Override
 99         public void onStartPage(PdfWriter writer, Document document) {
100             try{
101                 this.template = writer.getDirectContent().createTemplate(100, 100);
102                 this.baseFont = new Font(Resource.BASE_FONT_CHINESE , 8, Font.NORMAL).getBaseFont();
103             } catch(Exception e) {
104                 throw new ExceptionConverter(e);
105             }
106         }
107         
108         @Override
109         public void onEndPage(PdfWriter writer, Document document) {
110             //在每頁結束的時候把「第x頁」信息寫道模版指定位置
111             PdfContentByte byteContent = writer.getDirectContent();
112             String text = "第" + writer.getPageNumber() + "頁";
113             float textWidth = this.baseFont.getWidthPoint(text, 8);
114             float realWidth = document.right() - textWidth;
115             //
116             byteContent.beginText();
117             byteContent.setFontAndSize(this.baseFont , 10);
118             byteContent.setTextMatrix(realWidth , document.bottom());
119             byteContent.showText(text);
120             byteContent.endText();
121             byteContent.addTemplate(this.template , realWidth , document.bottom());
122         }
123     }  
124 }
Excel2Pdf
  1 public class PdfTableExcel {
  2     //ExcelObject
  3     protected ExcelObject excelObject;
  4     //excel
  5     protected Excel excel;
  6     //
  7     protected boolean setting = false;
  8     
  9     /**
 10      * <p>Description: Constructor</p>
 11      * @param excel
 12      */
 13     public PdfTableExcel(ExcelObject excelObject){
 14         this.excelObject = excelObject;
 15         this.excel = excelObject.getExcel();
 16     }
 17     
 18     /**
 19      * <p>Description: 獲取轉換過的Excel內容Table</p>
 20      * @return PdfPTable
 21      * @throws BadElementException
 22      * @throws MalformedURLException
 23      * @throws IOException
 24      */
 25     public PdfPTable getTable() throws BadElementException, MalformedURLException, IOException{
 26         Sheet sheet = this.excel.getSheet();
 27         return toParseContent(sheet);
 28     }
 29     
 30     protected PdfPTable toParseContent(Sheet sheet) throws BadElementException, MalformedURLException, IOException{
 31         int rowlength = sheet.getLastRowNum();
 32         List<PdfPCell> cells = new ArrayList<PdfPCell>();
 33         float[] widths = null;
 34         float mw = 0;
 35         for (int i = 0; i < rowlength; i++) {
 36             Row row = sheet.getRow(i);
 37             float[] cws = new float[row.getLastCellNum()];
 38             for (int j = 0; j < row.getLastCellNum(); j++) {
 39                 Cell cell = row.getCell(j);
 40                 float cw = getPOIColumnWidth(cell);
 41                 cws[cell.getColumnIndex()] = cw;
 42                 if(isUsed(cell.getColumnIndex(), row.getRowNum())){
 43                     continue;
 44                 }
 45                 cell.setCellType(Cell.CELL_TYPE_STRING);
 46                 CellRangeAddress range = getColspanRowspanByExcel(row.getRowNum(), cell.getColumnIndex());
 47                 //
 48                 int rowspan = 1;
 49                 int colspan = 1;
 50                 if (range != null) {
 51                     rowspan = range.getLastRow() - range.getFirstRow() + 1;
 52                     colspan = range.getLastColumn() - range.getFirstColumn() + 1;
 53                 }
 54                 //PDF單元格
 55                 PdfPCell pdfpCell = new PdfPCell();
 56                 pdfpCell.setBackgroundColor(new BaseColor(getBackgroundColorByExcel(cell.getCellStyle())));
 57                 pdfpCell.setColspan(colspan);
 58                 pdfpCell.setRowspan(rowspan);
 59                 pdfpCell.setVerticalAlignment(getVAlignByExcel(cell.getCellStyle().getVerticalAlignment()));
 60                 pdfpCell.setHorizontalAlignment(getHAlignByExcel(cell.getCellStyle().getAlignment()));
 61                 pdfpCell.setPhrase(getPhrase(cell));
 62                 pdfpCell.setFixedHeight(this.getPixelHeight(row.getHeightInPoints()));
 63                 addBorderByExcel(pdfpCell, cell.getCellStyle());
 64                 addImageByPOICell(pdfpCell , cell , cw);
 65                 //
 66                 cells.add(pdfpCell);
 67                 j += colspan - 1;
 68             }
 69             float rw = 0;
 70             for (int j = 0; j < cws.length; j++) {
 71                 rw += cws[j];
 72             }
 73             if (rw > mw ||  mw == 0) {
 74                 widths = cws;
 75                 mw = rw;
 76             }
 77         }
 78         //
 79         PdfPTable table = new PdfPTable(widths);
 80         table.setWidthPercentage(100);
 81 //        table.setLockedWidth(true);
 82         for (PdfPCell pdfpCell : cells) {
 83             table.addCell(pdfpCell);
 84         }
 85         return table;
 86     }
 87     
 88     protected Phrase getPhrase(Cell cell){
 89         if(this.setting || this.excelObject.getAnchorName() == null){
 90            return new Phrase(cell.getStringCellValue(), getFontByExcel(cell.getCellStyle()));
 91         }
 92         Anchor anchor = new Anchor(cell.getStringCellValue() , getFontByExcel(cell.getCellStyle()));
 93         anchor.setName(this.excelObject.getAnchorName());
 94         this.setting = true;
 95         return anchor;
 96     }
 97     
 98     protected void addImageByPOICell(PdfPCell pdfpCell , Cell cell , float cellWidth) throws BadElementException, MalformedURLException, IOException{
 99        POIImage poiImage = new POIImage().getCellImage(cell);
100        byte[] bytes = poiImage.getBytes();
101        if(bytes != null){
102 //           double cw = cellWidth;
103 //           double ch = pdfpCell.getFixedHeight();
104 //           
105 //           double iw = poiImage.getDimension().getWidth();
106 //           double ih = poiImage.getDimension().getHeight();
107 //           
108 //           double scale = cw / ch;
109 //           
110 //           double nw = iw * scale;
111 //           double nh = ih - (iw - nw);
112 //           
113 //           POIUtil.scale(bytes , nw  , nh);
114            pdfpCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
115            pdfpCell.setHorizontalAlignment(Element.ALIGN_CENTER);
116            Image image = Image.getInstance(bytes);
117            pdfpCell.setImage(image);
118        }
119     }
120     
121     protected float getPixelHeight(float poiHeight){
122         float pixel = poiHeight / 28.6f * 26f;
123         return pixel;
124     }
125     
126     /**
127      * <p>Description: 此處獲取Excel的列寬像素(沒法精確實現,期待有能力的朋友進行改善此處)</p>
128      * @param cell 
129      * @return 像素寬
130      */
131     protected int getPOIColumnWidth(Cell cell) {
132         int poiCWidth = excel.getSheet().getColumnWidth(cell.getColumnIndex());
133         int colWidthpoi = poiCWidth;
134         int widthPixel = 0;
135         if (colWidthpoi >= 416) {
136             widthPixel = (int) (((colWidthpoi - 416.0) / 256.0) * 8.0 + 13.0 + 0.5);
137         } else {
138             widthPixel = (int) (colWidthpoi / 416.0 * 13.0 + 0.5);
139         }
140         return widthPixel;
141     }
142     
143     protected CellRangeAddress getColspanRowspanByExcel(int rowIndex, int colIndex) {
144         CellRangeAddress result = null;
145         Sheet sheet = excel.getSheet();
146         int num = sheet.getNumMergedRegions();
147         for (int i = 0; i < num; i++) {
148             CellRangeAddress range = sheet.getMergedRegion(i);
149             if (range.getFirstColumn() == colIndex && range.getFirstRow() == rowIndex) {
150                 result = range;
151             }
152         }
153         return result;
154     }
155     
156     protected boolean isUsed(int colIndex , int rowIndex){
157         boolean result = false;
158         Sheet sheet = excel.getSheet();
159         int num = sheet.getNumMergedRegions();
160         for (int i = 0; i < num; i++) {
161             CellRangeAddress range = sheet.getMergedRegion(i);
162             int firstRow = range.getFirstRow();
163             int lastRow = range.getLastRow();
164             int firstColumn = range.getFirstColumn();
165             int lastColumn = range.getLastColumn();
166             if (firstRow < rowIndex && lastRow >= rowIndex) {
167                 if(firstColumn <= colIndex && lastColumn >= colIndex){
168                     result = true;
169                 }
170             }
171         }
172         return result;
173     }
174 
175     protected Font getFontByExcel(CellStyle style) {
176        Font result = new Font(Resource.BASE_FONT_CHINESE , 8 , Font.NORMAL);
177        Workbook wb = excel.getWorkbook();
178        //字體樣式索引
179        short index = style.getFontIndex();
180        org.apache.poi.ss.usermodel.Font font = wb.getFontAt(index);
181        //字體顏色
182        int colorIndex = font.getColor();
183        if(font.getBoldweight() == org.apache.poi.ss.usermodel.Font.BOLDWEIGHT_BOLD){
184            result.setStyle(Font.BOLD);
185        }
186        HSSFColor color = HSSFColor.getIndexHash().get(colorIndex);
187        if(color != null){
188            int rbg = POIUtil.getRGB(color);
189            result.setColor(new BaseColor(rbg));
190        }
191        //下劃線
192        FontUnderline underline = FontUnderline.valueOf(font.getUnderline());
193        if(underline == FontUnderline.SINGLE){
194            String ulString = FontStyle.UNDERLINE.getValue();
195            result.setStyle(ulString);
196        }
197        return result;
198     }
199     
200     protected int getBackgroundColorByExcel(CellStyle style) {
201         Color color = style.getFillForegroundColorColor();
202         return POIUtil.getRGB(color);
203     }
204     
205     protected void addBorderByExcel(PdfPCell cell , CellStyle style) {
206         Workbook wb = excel.getWorkbook();
207         cell.setBorderColorLeft(new BaseColor(POIUtil.getBorderRBG(wb,style.getLeftBorderColor())));
208         cell.setBorderColorRight(new BaseColor(POIUtil.getBorderRBG(wb,style.getRightBorderColor())));
209         cell.setBorderColorTop(new BaseColor(POIUtil.getBorderRBG(wb,style.getTopBorderColor())));
210         cell.setBorderColorBottom(new BaseColor(POIUtil.getBorderRBG(wb,style.getBottomBorderColor())));
211     }
212     
213     protected int getVAlignByExcel(short align) {
214         int result = 0;
215         if (align == CellStyle.VERTICAL_BOTTOM) {
216             result = Element.ALIGN_BOTTOM;
217         }
218         if (align == CellStyle.VERTICAL_CENTER) {
219             result = Element.ALIGN_MIDDLE;
220         }
221         if (align == CellStyle.VERTICAL_JUSTIFY) {
222             result = Element.ALIGN_JUSTIFIED;
223         }
224         if (align == CellStyle.VERTICAL_TOP) {
225             result = Element.ALIGN_TOP;
226         }
227         return result;
228     }
229 
230     protected int getHAlignByExcel(short align) {
231         int result = 0;
232         if (align == CellStyle.ALIGN_LEFT) {
233             result = Element.ALIGN_LEFT;
234         }
235         if (align == CellStyle.ALIGN_RIGHT) {
236             result = Element.ALIGN_RIGHT;
237         }
238         if (align == CellStyle.ALIGN_JUSTIFY) {
239             result = Element.ALIGN_JUSTIFIED;
240         }
241         if (align == CellStyle.ALIGN_CENTER) {
242             result = Element.ALIGN_CENTER;
243         }
244         return result;
245     }
246 }
PdfTableExcel
 1 public class Resource {
 2     /**
 3      * 中文字體支持
 4      */
 5     protected static BaseFont BASE_FONT_CHINESE;
 6     static {
 7         try {
 8             BASE_FONT_CHINESE = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
 9         } catch (Exception e) {
10             e.printStackTrace();
11         }
12     }
13 }
Resource
 1 public class POIImage {
 2     protected Dimension dimension;
 3     protected byte[] bytes;
 4     protected ClientAnchor anchor;
 5     
 6     public POIImage getCellImage(Cell cell) {
 7         byte[] result = null;
 8         Sheet sheet = cell.getSheet();
 9 //      Workbook wb = sheet.getWorkbook();
10 //      List<PictureData> pictures = (List<PictureData>) wb.getAllPictures();
11         if (sheet instanceof HSSFSheet) {
12             HSSFSheet hssfSheet = (HSSFSheet) sheet;
13             List<HSSFShape> shapes = hssfSheet.getDrawingPatriarch().getChildren();
14             for (HSSFShape shape : shapes) {
15                 HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
16                 if (shape instanceof HSSFPicture) {
17                     HSSFPicture pic = (HSSFPicture) shape;
18                     PictureData data = pic.getPictureData();
19                     String extension = data.suggestFileExtension();
20                     int row1 = anchor.getRow1();
21                     int row2 = anchor.getRow2();
22                     int col1 = anchor.getCol1();
23                     int col2 = anchor.getCol2();
24                     if(row1 == cell.getRowIndex() && col1 == cell.getColumnIndex()){
25                         dimension = pic.getImageDimension();
26                         this.anchor = anchor;
27                         this.bytes = data.getData();
28                     }
29                 }
30             }
31         }
32         return this;
33     }
34     
35     public Dimension getDimension() {
36         return dimension;
37     }
38 
39     public void setDimension(Dimension dimension) {
40         this.dimension = dimension;
41     }
42 
43     public byte[] getBytes() {
44         return bytes;
45     }
46     
47     public void setBytes(byte[] bytes) {
48         this.bytes = bytes;
49     }
50     
51     public ClientAnchor getAnchor() {
52         return anchor;
53     }
54 
55     public void setAnchor(ClientAnchor anchor) {
56         this.anchor = anchor;
57     }
58 }
POIImage
 1 public class POIUtil {
 2     
 3     public static int getRGB(Color color){
 4         int result = 0x00FFFFFF;
 5         
 6         int red = 0;
 7         int green = 0;
 8         int blue = 0;
 9         
10         if (color instanceof HSSFColor) {
11             HSSFColor hssfColor = (HSSFColor) color;
12             short[] rgb = hssfColor.getTriplet();
13             red = rgb[0];
14             green = rgb[1];
15             blue = rgb[2];
16         }
17         
18         if (color instanceof XSSFColor) {
19             XSSFColor xssfColor = (XSSFColor) color;
20             byte[] rgb = xssfColor.getRgb();
21             red = (rgb[0] < 0) ? (rgb[0] + 256) : rgb[0];
22             green = (rgb[1] < 0) ? (rgb[1] + 256) : rgb[1];
23             blue = (rgb[2] < 0) ? (rgb[2] + 256) : rgb[2];
24         }
25         
26         if(red != 0 || green != 0 || blue != 0){
27             result = new java.awt.Color(red, green, blue).getRGB();
28         }
29         return result;
30     }
31     
32     public static int getBorderRBG(Workbook wb  , short index){
33         int result = 0;
34         
35         if(wb instanceof HSSFWorkbook){
36             HSSFWorkbook hwb = (HSSFWorkbook)wb;
37             HSSFColor color =  hwb.getCustomPalette().getColor(index);
38             if(color != null){
39                 result = getRGB(color);
40             }
41         }
42         
43         if(wb instanceof XSSFWorkbook){
44             XSSFColor color = new XSSFColor();
45             color.setIndexed(index);
46             result = getRGB(color);
47         }
48         
49         return result;
50     }
51     
52     @SuppressWarnings("finally")
53     public static byte[] scale(byte[] bytes , double width, double height) {
54         BufferedImage bufferedImage = null;
55         BufferedImage bufTarget = null;
56         try {
57             ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
58             bufferedImage = ImageIO.read(bais);
59             double sx =  width / bufferedImage.getWidth();
60             double sy =  height / bufferedImage.getHeight();
61             int type = bufferedImage.getType();
62             if (type == BufferedImage.TYPE_CUSTOM) {
63                 ColorModel cm = bufferedImage.getColorModel();
64                 WritableRaster raster = cm.createCompatibleWritableRaster((int)width, (int)height);
65                 boolean alphaPremultiplied = cm.isAlphaPremultiplied();
66                 bufTarget = new BufferedImage(cm, raster, alphaPremultiplied, null);
67             } else {
68                 bufTarget = new BufferedImage((int)width, (int)height, type);
69             }
70             
71             Graphics2D g = bufTarget.createGraphics();
72             g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
73             g.drawRenderedImage(bufferedImage, AffineTransform.getScaleInstance(sx, sy));
74             g.dispose();
75             
76             if(bufTarget != null){
77                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
78                 ImageIO.write(bufTarget, "png", baos);
79                 byte[] result = baos.toByteArray();
80                 return result;
81             }
82         } catch (IOException e) {
83             e.printStackTrace();
84         }
85         return null;
86     }
87 }
POIUtil
 1 public static void main(String[] args) throws Exception {
 2 //      FileInputStream fis = new FileInputStream(new File(directory+"副本Common-ReportList-AssistPDF-Temp.xls"));
 3 //      FileInputStream fis = new FileInputStream(new File("D:\\tmp.xls"));
 4         
 5         FileInputStream fis1 = new FileInputStream(new File("D:\\pdfexport\\MAD 5-3-05-Octavia NF-20131025.xls"));
 6         FileInputStream fis2 = new FileInputStream(new File("D:\\pdfexport\\MAD 6-1-47-Octavia NF-20131025.xls"));
 7         FileInputStream fis3 = new FileInputStream(new File("D:\\pdfexport\\MAD 038-Superb FL DS-20131025.xls"));
 8         //
 9         FileOutputStream fos = new FileOutputStream(new File("D:\\test.pdf"));
10         //
11         List<ExcelObject> objects = new ArrayList<ExcelObject>();
12         objects.add(new ExcelObject("1.MAD 5-3-05-Octavia NF-20131025.xls",fis1));
13         objects.add(new ExcelObject("2.MAD 6-1-47-Octavia NF-20131025.xls",fis2));
14         objects.add(new ExcelObject("3.MAD 038-Superb FL DS-20131025.xls",fis3));
15 //        
16         Excel2Pdf pdf = new Excel2Pdf(objects , fos);
17         pdf.convert();
18     }
Main(入口代碼) 

代碼完畢,若是有什麼問題的能夠給我留言,你們能夠共同探討一下!

相關文章
相關標籤/搜索