package
k.barcode;
002 |
003 | import java.awt.image.BufferedImage; |
004 | import java.io.File; |
005 | import java.io.FileInputStream; |
006 | import java.io.FileNotFoundException; |
007 | import java.io.FileOutputStream; |
008 | import java.io.IOException; |
009 | import java.io.InputStream; |
010 | import java.io.OutputStream; |
011 |
012 | import org.apache.poi.hssf.usermodel.HSSFRow; |
013 | import org.apache.poi.hssf.usermodel.HSSFSheet; |
014 | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
015 | import org.apache.poi.poifs.filesystem.POIFSFileSystem; |
016 | import org.apache.poi.ss.usermodel.ClientAnchor; |
017 | import org.apache.poi.ss.usermodel.CreationHelper; |
018 | import org.apache.poi.ss.usermodel.Drawing; |
019 | import org.apache.poi.ss.usermodel.Picture; |
020 | import org.apache.poi.ss.usermodel.Workbook; |
021 | import org.apache.poi.util.IOUtils; |
022 | import org.krysalis.barcode4j.impl.code39.Code39Bean; |
023 | import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider; |
024 | import org.krysalis.barcode4j.tools.UnitConv; |
025 |
026 | public class InsExcel { |
027 |
028 | private POIFSFileSystem fs; |
029 | private HSSFWorkbook wb; |
030 | private HSSFSheet sheet; |
031 | private HSSFRow row; |
032 | private FileInputStream input; |
033 | private String[] excleTitle; |
034 |
035 | public static 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 | public static String makeBarcodeValue(){ |
046 | return "A" +System.currentTimeMillis(); |
047 | } |
048 | |
049 | //生成条码文件至临时目录,并返回生成图片的路径信息 |
050 | public static String makeBarcode( final String barcodeValue){ |
051 | //存放条码图片的路径 |
052 | final String barcodePicPath = "d:\\barcode\\" ; |
053 | try { |
054 | //Create the barcode bean |
055 | Code39Bean bean = new Code39Bean(); |
056 | |
057 | final int 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 | return barcodePicPath+barcodeValue+ ".png" ; |
086 | } |
087 | |
088 | public static void insBarcodeInExcel(String barcodePic){ |
089 | FileInputStream input = null ; |
090 | try { |
091 | input = new FileInputStream( new File( "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 | int pictureIdx = 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 | } |