动态生成条形码并将条形码插入进excel中

来源:互联网 发布:mac口红a17是什么颜色 编辑:程序博客网 时间:2024/06/11 17:57
package k.barcode;
002 
003import java.awt.image.BufferedImage;
004import java.io.File;
005import java.io.FileInputStream;
006import java.io.FileNotFoundException;
007import java.io.FileOutputStream;
008import java.io.IOException;
009import java.io.InputStream;
010import java.io.OutputStream;
011 
012import org.apache.poi.hssf.usermodel.HSSFRow;
013import org.apache.poi.hssf.usermodel.HSSFSheet;
014import org.apache.poi.hssf.usermodel.HSSFWorkbook;
015import org.apache.poi.poifs.filesystem.POIFSFileSystem;
016import org.apache.poi.ss.usermodel.ClientAnchor;
017import org.apache.poi.ss.usermodel.CreationHelper;
018import org.apache.poi.ss.usermodel.Drawing;
019import org.apache.poi.ss.usermodel.Picture;
020import org.apache.poi.ss.usermodel.Workbook;
021import org.apache.poi.util.IOUtils;
022import org.krysalis.barcode4j.impl.code39.Code39Bean;
023import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
024import org.krysalis.barcode4j.tools.UnitConv;
025 
026public classInsExcel {
027 
028    privatePOIFSFileSystem fs;
029    privateHSSFWorkbook wb;
030    privateHSSFSheet sheet;
031    privateHSSFRow row;
032    privateFileInputStream input;
033    privateString[] excleTitle;
034 
035    publicstatic void main(String[] args) {
036        //生成条码
037        String bcVal = InsExcel.makeBarcodeValue();
038        //生成条码图片
039        String bcPath = InsExcel.makeBarcode(bcVal);
040        //插入excel
041        InsExcel.insBarcodeInExcel(bcPath);
042    }
043     
044    //生成条码值
045    publicstatic String makeBarcodeValue(){
046        return"A"+System.currentTimeMillis();
047    }
048     
049    //生成条码文件至临时目录,并返回生成图片的路径信息
050    publicstatic String makeBarcode(finalString barcodeValue){
051        //存放条码图片的路径
052        finalString barcodePicPath = "d:\\barcode\\";
053        try{
054            //Create the barcode bean
055            Code39Bean bean =new Code39Bean();
056             
057            finalint dpi = 150;
058             
059            //Configure the barcode generator
060            bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi));//makes the narrow bar
061                                                             //width exactly one pixel
062            bean.setWideFactor(3);
063            bean.doQuietZone(false);
064             
065            //Open output file
066            File outputFile =new File(barcodePicPath+barcodeValue+".png");
067            OutputStream out =new FileOutputStream(outputFile);
068            try{
069                //Set up the canvas provider for monochrome JPEG output
070                BitmapCanvasProvider canvas =new BitmapCanvasProvider(
071                        out,"image/png", dpi, BufferedImage.TYPE_BYTE_BINARY,false, 0);
072             
073                //Generate the barcode
074                bean.generateBarcode(canvas, barcodeValue);
075             
076                //Signal end of generation
077                canvas.finish();
078            }finally {
079                out.close();
080            }
081        } catch (Exception e) {
082            e.printStackTrace();
083        }
084         
085        returnbarcodePicPath+barcodeValue+".png";
086    }
087     
088    publicstatic void insBarcodeInExcel(String barcodePic){
089        FileInputStream input =null;
090        try{
091            input =new FileInputStream(newFile("d:\\base.xls"));// excelPath,Excel
092            // 文件 的绝对路径
093            POIFSFileSystem fs =new POIFSFileSystem(input);
094            HSSFWorkbook wb =new HSSFWorkbook(fs);
095            HSSFSheet sheet = wb.getSheetAt(1);
096            HSSFRow row = sheet.getRow(0);// 得到标题的内容对象。
097            System.out.println(row.getCell(15).toString());
098 
099            InputStream inputStream =new FileInputStream(barcodePic);
100            byte[] bytes = IOUtils.toByteArray(inputStream);
101            intpictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
102            inputStream.close();
103            CreationHelper helper = wb.getCreationHelper();
104            Drawing drawing = sheet.createDrawingPatriarch();
105            ClientAnchor anchor = helper.createClientAnchor();
106            anchor.setCol1(15);
107            anchor.setRow1(0);
108            Picture pict = drawing.createPicture(anchor, pictureIdx);
109            pict.resize();
110            FileOutputStream fileOut =new FileOutputStream("d:\\base.xls");
111            wb.write(fileOut);
112            fileOut.close();
113 
114        } catch (FileNotFoundException e) {
115            e.printStackTrace();
116        } catch (IOException e) {
117            e.printStackTrace();
118        } finally {
119            try{
120                if(input != null) {
121                    input.close();
122                }
123            }catch (IOException e) {
124                e.printStackTrace();
125            }
126        }
127    }
128 
129}
0 0
原创粉丝点击