1. 程式人生 > >java在excel裡插入文字資料和圖片(JXL方式)

java在excel裡插入文字資料和圖片(JXL方式)

如題所示,這裡講述下如何通過JXL包在excel裡插入文字資料以及圖片。

這裡先展示下後續文章裡要提到的1.xls(模板檔案)和2.xls(匯出檔案)

        1.xls:


2.xls(空白的excel):


要匯入的圖片(320*240):

附上程式碼:

<span style="font-size:14px;">package com.test.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import org.junit.Test;

public class ExcelTest {
    @Test
    public void portExcel() {
        OutputStream os = null;
        WritableWorkbook wbook = null;
        InputStream is = null;

        try {
            //獲取輸入流
            is = new FileInputStream("C:/Users/test/Desktop/1.xls");
            //獲取輸出流
            os = new FileOutputStream("C:/Users/test/Desktop/2.xls");
            //建立WritableWorkbook物件
            wbook = Workbook.createWorkbook(os, Workbook.getWorkbook(is));
            //獲取sheet物件
            WritableSheet sheet = wbook.getSheet(0);
            //初始化行
            int row = 0;
            //初始化列
            int col = 0;
            col = 0;
            row++;
            //圖片檔案
            /**
             * <span style="color:#ff0000;">注意:jxl裡插入圖片,圖片必須為png格式</span>
             * (我這裡圖片比例為4:3, 圖片那列,列寬是在1.xls裡設定好的,行高在程式碼裡設定)
             */
            File imgFile = new File("C:/Users/test/Desktop/456.png");

            sheet.addCell(new Label(col++, row, "123456789"));
            sheet.addCell(new Label(col++, row, "張三"));
            sheet.addCell(new Label(col++, row, "男"));
            sheet.addCell(new Label(col++, row, "37053318580536451X"));
            sheet.addCell(new Label(col++, row, "李四"));
            sheet.addCell(new Label(col++, row, "魯A99999"));
            sheet.addCell(new Label(col++, row, "18888888888"));
            sheet.addCell(new Label(col++, row, "2016-04-12 14:09:09"));
            sheet.addCell(new Label(col++, row, "2016-05-15 13:11:45"));
            sheet.addCell(new Label(col++, row, "來此廠參觀"));
            
            
            /**WritableImage(double x,double y,double width,double height,java.io.File image)
             * x - the column number at which to position the image 圖片左上角位置為多少列
             * y - the row number at which to position the image 圖片左上角位置為多少行
             * width - the number of columns cells which the image spans 圖片寬度在excel裡佔據多少列
             * height - the number of rows which the image spans 圖片寬度在excel裡佔據多少行
             * image - the source image file 圖片檔案地址
             * */
            WritableImage image = new WritableImage(col++, row, 1, 1, imgFile);
            
            sheet.setRowView(row, 1700, false); //設定行高
            // 把圖片插入到sheet
            sheet.addImage(image);
            
//          如果需要插入多張圖片,則按下面的方式繼續新增
//          WritableImage image2 = new WritableImage(10, 2, 1, 1, imgFile);
//          sheet.addImage(image);
            //設定行高
            wbook.write();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (wbook != null) {
                    wbook.close();
                }
                if (os != null) {
                    os.flush();
                    os.close();
                }
            } catch (WriteException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}</span>
最後附上執行以上程式碼後的2.xls(因圖片上傳到部落格裡有點壓縮,實際效果圖片在單元格里以4:3的比例顯示):