利用poi如何将条形码写入word中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用poi如何将条形码写入word中相关的知识,希望对你有一定的参考价值。

参考技术A

/**条形码工具类
*/
public class OneBarcodeUtil 

public void tool(String str)

try

JBarcode localJBarcode = new JBarcode(EAN13Encoder.getInstance(), WidthCodedPainter.getInstance(), EAN13TextPainter.getInstance());
//生成. 欧洲商品条码(=European Article Number)
//这里我们用作图书条码  
//      String str = "788515004012";
BufferedImage localBufferedImage = localJBarcode.createBarcode(str);
saveToGIF(localBufferedImage, "EAN13.gif");
localJBarcode.setEncoder(Code39Encoder.getInstance());
localJBarcode.setPainter(WideRatioCodedPainter.getInstance());
localJBarcode.setTextPainter(BaseLineTextPainter.getInstance());
localJBarcode.setShowCheckDigit(false);
//xx  
//      str = "JBARCODE-39";  
//      localBufferedImage = localJBarcode.createBarcode(str);  
//      saveToPNG(localBufferedImage, "Code39.png");


catch (Exception localException)

localException.printStackTrace();



static void saveToJPEG(BufferedImage paramBufferedImage, String paramString)

saveToFile(paramBufferedImage, paramString, "jpeg");


static void saveToPNG(BufferedImage paramBufferedImage, String paramString)

saveToFile(paramBufferedImage, paramString, "png");


static void saveToGIF(BufferedImage paramBufferedImage, String paramString)

saveToFile(paramBufferedImage, paramString, "gif");


static void saveToFile(BufferedImage paramBufferedImage, String paramString1, String paramString2)

try

FileOutputStream localFileOutputStream = new FileOutputStream("d:/images/" + paramString1);
ImageUtil.encodeAndWrite(paramBufferedImage, paramString2, localFileOutputStream, 96, 96);
localFileOutputStream.close();

catch (Exception localException)

localException.printStackTrace();



  
/**
*word设置文档格式
*/
XWPFDocument xdoc = new XWPFDocument();

XWPFParagraph p = xdoc.createParagraph();
// 固定值25磅
setParagraphSpacingInfo(p, true, "0", "150", null, null, true, "500",
STLineSpacingRule.EXACT);
// 左对齐
setParagraphAlignInfo(p, ParagraphAlignment.LEFT,
TextAlignment.CENTER);
XWPFRun pRun = getOrAddParagraphFirstRun(p, false, false);
//     setParagraphRunFontInfo(p, pRun, "**********调度单", "黑体",
//         "Times New Roman", "24", false, false, false, false, null, null,
//         0, 0, 90);
setParagraphRunFontInfo(p, pRun, "oneBarcodeUtil.tool(waybill.getWaybillNo())", "黑体",
"Times New Roman", "24", false, false, false, false, null, null,
0, 0, 90);
p = xdoc.createParagraph();
// 单倍行距
setParagraphSpacingInfo(p, true, "0", "0", "0", "0", true, "240",
STLineSpacingRule.AUTO);
setParagraphAlignInfo(p, ParagraphAlignment.LEFT, TextAlignment.CENTER);
pRun = getOrAddParagraphFirstRun(p, false, false);
setParagraphRunFontInfo(p, pRun, "调度类型:"+departure.getDepartureType()+"制单人:"+departure.getCreator()+"打印时间:"+departure.getCreateTime(), "宋体",
"Times New Roman", "15", false, false, false, false, null, null,
10, 6, 0);
如何将条形码输入:以下是要实现的效果

java利用poi导出word文档

项目中,有时候需要使用poi实现将固定数据导入word中
效果图:

代码:

package poiword;

import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;

public class TestPOIWordOut 
    public static void main(String[] args) 

        //Blank Document  
        XWPFDocument document= new XWPFDocument();  

        //Write the Document in file system  
        FileOutputStream out;
        try 
            out = new FileOutputStream(new File("C:/Users/Administrator/Desktop/poi_word.docx"));

        //添加标题  
        XWPFParagraph titleParagraph = document.createParagraph();  
        //设置段落居中  
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);  

        XWPFRun titleParagraphRun = titleParagraph.createRun();  
        titleParagraphRun.setText("自学指导(标题)");  
        titleParagraphRun.setColor("000000");  
        titleParagraphRun.setFontSize(20);  

        //段落  
        XWPFParagraph firstParagraph = document.createParagraph();  
        XWPFRun run = firstParagraph.createRun();  
        run.setText("   自学遇到问题怎么办?");  
        run.setColor("698869");  
        run.setFontSize(16);  

        //设置段落背景颜色  
        CTShd cTShd = run.getCTR().addNewRPr().addNewShd();  
        cTShd.setVal(STShd.CLEAR);  
        cTShd.setFill("97FF45");  

        //换行  
        XWPFParagraph paragraph1 = document.createParagraph();  
        XWPFRun paragraphRun1 = paragraph1.createRun();  
        paragraphRun1.setText("\\r");  

        //基本信息表格  
        XWPFTable infoTable = document.createTable();  
        //去表格边框  
        infoTable.getCTTbl().getTblPr().unsetTblBorders();  

        //列宽自动分割  
        CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();  
        infoTableWidth.setType(STTblWidth.DXA);  
        infoTableWidth.setW(BigInteger.valueOf(9072));  

        //表格第一行  
        XWPFTableRow infoTableRowOne = infoTable.getRow(0);  
        infoTableRowOne.getCell(0).setText("职位");  
        infoTableRowOne.addNewTableCell().setText(": xxxx");  

        //表格第二行  
        XWPFTableRow infoTableRowTwo = infoTable.createRow();  
        infoTableRowTwo.getCell(0).setText("姓名");  
        infoTableRowTwo.getCell(1).setText(": xxxx");  

        //表格第三行  
        XWPFTableRow infoTableRowThree = infoTable.createRow();  
        infoTableRowThree.getCell(0).setText("生日");  
        infoTableRowThree.getCell(1).setText(": xxx-xx-xx");  

        //表格第四行  
        XWPFTableRow infoTableRowFour = infoTable.createRow();  
        infoTableRowFour.getCell(0).setText("性别");  
        infoTableRowFour.getCell(1).setText(": x");  

        //表格第五行  
        XWPFTableRow infoTableRowFive = infoTable.createRow();  
        infoTableRowFive.getCell(0).setText("现居地");  
        infoTableRowFive.getCell(1).setText(": xx");  

        //两个表格之间加个换行  
        XWPFParagraph paragraph = document.createParagraph();  
        XWPFRun paragraphRun = paragraph.createRun();  
        paragraphRun.setText("\\r");  

        //工作经历表格  
        XWPFTable ComTable = document.createTable();  

        //列宽自动分割  
        CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();  
        comTableWidth.setType(STTblWidth.DXA);  
        comTableWidth.setW(BigInteger.valueOf(9072)); 

        //表格第一行  
        XWPFTableRow comTableRowOne = ComTable.getRow(0);  
        comTableRowOne.getCell(0).setText("开始时间");  
        comTableRowOne.addNewTableCell().setText("结束时间");  
        comTableRowOne.addNewTableCell().setText("公司名称");  
        comTableRowOne.addNewTableCell().setText("职位");  

        //表格第二行  
        XWPFTableRow comTableRowTwo = ComTable.createRow();  
        comTableRowTwo.getCell(0).setText("20xx-xx-xx");  
        comTableRowTwo.getCell(1).setText("至今");  
        comTableRowTwo.getCell(2).setText("自学指导网站");  
        comTableRowTwo.getCell(3).setText("Java开发工程师");  

        //表格第三行  
        XWPFTableRow comTableRowThree = ComTable.createRow();  
        comTableRowThree.getCell(0).setText("20xx-xx-xx");  
        comTableRowThree.getCell(1).setText("至今");  
        comTableRowThree.getCell(2).setText("s.jf3q.com");  
        comTableRowThree.getCell(3).setText("Java开发工程师");  

            document.write(out);  
            out.close();  
         catch (Exception e1) 
            // TODO Auto-generated catch block
            e1.printStackTrace();
         
        System.out.println("create_table document written success."); 
    

注意事项:最核心的就是需要的jar包不能下载错哦。

以上是关于利用poi如何将条形码写入word中的主要内容,如果未能解决你的问题,请参考以下文章

用java程序如何读取图书条形码

asp.net 导出word文档在导出这个word文档的某一书签位置生成一个条形码

条形码java代码问题 菜鸟级问题

java怎么条形码识别,识别图片中的条形码。

java识别条形码api都有哪些

怎么样用java生产ITF条形码。。