POI 从控制台读取表格每行信息,通过POI向word写入表格

其他博客里有个表格如下:

三、 直接标志转移
指令格式                 机器码                    测试条件                       如...则转移  
JC                           72                           C=1                               有进位
JNC                         73                          C=0                               无进位
JZ/JE                       74                          Z=1                                零/  
JNZ/JNE                  75                         Z=0                               不为零/  
JS                           78                           S=1                                负号
JNS                         79                          S=0                                正号
JO                           70                          O=1                               有溢出
JNO                         71                         O=0                               无溢出
JP/JPE                     7A                        P=1                               奇偶位为偶
JNP/IPO                   7B                        P=0                               奇偶位为奇


现在来用POI来实现将上面的表格写入到word中

代码如下:


package my;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Pattern;
import org.apache.poi.hssf.record.SCLRecord;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

public class ReadTablesToWord
{
	public static void main(String[] args) throws IOException
	{
		//创建docx文档对象
        XWPFDocument document = new XWPFDocument();

        // Write the Document in file system
        FileOutputStream out = new FileOutputStream(
        		new File("C:\\Users\\Administrator\\Desktop\\code.docx"));
        // create table
        XWPFTable table = document.createTable();
        Scanner scanner=new Scanner(System.in);
        boolean hasRow=false;
        while(scanner.hasNextLine())
        {
        	String line=scanner.nextLine();
        	//再输入一个回车符就结束了。
        	if(line.isEmpty())
        		break;
        	
        	//以空白符分割
        	String[] words=line.split("\\s+");
        	XWPFTableRow tableRow;
        	if(!hasRow)
			{
		        tableRow = table.getRow(0);
			}
        	else
        	{
        		 tableRow = table.createRow();
        	}
        	for (int i=0;i<words.length;i++)
			{
        		//第一行操作
        		if(!hasRow)
        		{
        			System.out.println("-----------------第一行-----------------");
        			System.out.println(words[i]);
        			if(i==0)
        				tableRow.getCell(0).setText(words[i]);
        			else
        				tableRow.addNewTableCell().setText(words[i]);
        			System.out.println("-----------------第一行-----------------");
        		}
        		else 
        		{
        			System.out.println("-----------------第"+i+"行-----------------");
        			System.out.println(words[i]);
        			 tableRow.getCell(i).setText(words[i]);
        			System.out.println("-----------------第"+i+"行-----------------");
				}
			}
        	hasRow=true;
        }
        
        document.write(out);
        out.close();
	}
}


工程视图:



poi下载地址:http://poi.apache.org/download.html



运行结果:



可以看到生成的表格太丑了,自己按住表格右下角拖动调整就好啦。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值