关于JAVA处理excel和word

首先是Java读取excel的方法,可以参考下面方法

<pre name="code" class="java">import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.Borders;
import org.apache.poi.xwpf.usermodel.BreakClear;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.LineSpacingRule;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TextAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

/**
* A simple WOrdprocessingML document created by POI XWPF API
*
* @author Yegor Kozlov
*/
public class SimpleDocument2 {

 public static void main(String[] args) throws Exception {
     XWPFDocument doc = new XWPFDocument();

     XWPFParagraph p1 = doc.createParagraph();
     p1.setAlignment(ParagraphAlignment.CENTER);
     p1.setBorderBottom(Borders.DOUBLE);
     p1.setBorderTop(Borders.DOUBLE);

     p1.setBorderRight(Borders.DOUBLE);
     p1.setBorderLeft(Borders.DOUBLE);
     p1.setBorderBetween(Borders.SINGLE);

     p1.setVerticalAlignment(TextAlignment.TOP);

     XWPFRun r1 = p1.createRun();
     r1.setBold(true);
     r1.setText("-----");
     r1.setBold(true);
     r1.setFontFamily("Courier");
     r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
     r1.setTextPosition(100);

     XWPFParagraph p2 = doc.createParagraph();
     p2.setAlignment(ParagraphAlignment.RIGHT);

     //BORDERS
     p2.setBorderBottom(Borders.DOUBLE);
     p2.setBorderTop(Borders.DOUBLE);
     p2.setBorderRight(Borders.DOUBLE);
     p2.setBorderLeft(Borders.DOUBLE);
     p2.setBorderBetween(Borders.SINGLE);

     XWPFRun r2 = p2.createRun();
     r2.setText("jumped over the lazy dog");
     r2.setStrike(true);
     r2.setFontSize(20);

     XWPFRun r3 = p2.createRun();
     r3.setText("and went away");
     r3.setStrike(true);
     r3.setFontSize(20);
     r3.setSubscript(VerticalAlign.SUPERSCRIPT);


     XWPFParagraph p3 = doc.createParagraph();
     p3.setWordWrap(true);
     p3.setPageBreak(true);
             
     //p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
     p3.setAlignment(ParagraphAlignment.BOTH);
     p3.setSpacingLineRule(LineSpacingRule.EXACT);

     p3.setIndentationFirstLine(600);
     

     XWPFRun r4 = p3.createRun();
     r4.setTextPosition(20);
     r4.setText("To be, or not to be: that is the question: "
             + "Whether 'tis nobler in the mind to suffer "
             + "The slings and arrows of outrageous fortune, "
             + "Or to take arms against a sea of troubles, "
             + "And by opposing end them? To die: to sleep; ");
     r4.addBreak(BreakType.PAGE);
     r4.setText("No more; and by a sleep to say we end "
             + "The heart-ache and the thousand natural shocks "
             + "That flesh is heir to, 'tis a consummation "
             + "Devoutly to be wish'd. To die, to sleep; "
             + "To sleep: perchance to dream: ay, there's the rub; "
             + ".......");
     r4.setItalic(true);
//This would imply that this break shall be treated as a simple line break, and break the line after that word:

     XWPFRun r5 = p3.createRun();
     r5.setTextPosition(-10);
     r5.setText("For in that sleep of death what dreams may come");
     r5.addCarriageReturn();
     r5.setText("When we have shuffled off this mortal coil,"
             + "Must give us pause: there's the respect"
             + "That makes calamity of so long life;");
     r5.addBreak();
     r5.setText("For who would bear the whips and scorns of time,"
             + "The oppressor's wrong, the proud man's contumely,");
     
     r5.addBreak(BreakClear.ALL);
     r5.setText("The pangs of despised love, the law's delay,"
             + "The insolence of office and the spurns" + ".......");

     FileOutputStream out = new FileOutputStream("simple.docx");
     doc.write(out);
     out.close();

 }
}

Java写word的方法,可以参考
 
<pre name="code" class="java">import java.io.FileOutputStream;
import java.util.List;

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.TextAlignment;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
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.XWPFTableCell;

public class SimpleDocument {
	  
	  /**
	   * sd
	   */
	  public static void main(String[] args) throws Exception {
	    XWPFDocument doc = new XWPFDocument();
	    XWPFParagraph p1 = doc.createParagraph();
	    
	    XWPFTable table = doc.createTable(11, 2);
	    // 设置上下左右四个方向的距离,可以将表格撑大
	    table.setCellMargins(20, 20, 20, 20);
	    // table.set
	    List<XWPFTableCell> tableCells = table.getRow(0).getTableCells();
	    tableCells.get(0).setText("第一行第一列的数据");
	    tableCells.get(1).setText("第一行第二列的数据");
	    
	    List<XWPFTableCell> tableCellsq = table.getRow(1).getTableCells();
	    tableCellsq.get(0).setText("第二行第二列的数据");
	    tableCellsq.get(1).setText("第二行第二列的数据");
	    
	    // 设置字体对齐方式
	    p1.setAlignment(ParagraphAlignment.CENTER);
	    p1.setVerticalAlignment(TextAlignment.TOP);
	    // 第一页要使用p1所定义的属性
	    XWPFRun r1 = p1.createRun();
	    // 设置字体是否加粗
	    r1.setBold(true);
	    r1.setFontSize(20);
	    // 设置使用何种字体
	    r1.setFontFamily("Courier");
	    // 设置上下两行之间的间距
	    r1.setTextPosition(20);
	    r1.setText("公司招聘会入场须知");
	    
	    // 设置个人信息
	    XWPFParagraph p2 = doc.createParagraph();
	    p2.setAlignment(ParagraphAlignment.LEFT);
	    XWPFRun r2 = p2.createRun();
	    r2.setTextPosition(15);
	    r2.setText("姓名" + "                    " + "张三");
	    r2.addCarriageReturn();
	    r2.setText("性别" + "                    " + "女");
	    r2.addCarriageReturn();
	    r2.setText("手机号" + "               " + "12345678965");
	    r2.addCarriageReturn();
	    r2.setText("邮箱" + "                    " + "123@163.com");
	    r2.addCarriageReturn();
	    r2.setText("开始时间" + "      " + "2013-05-28 12:30");
	    r2.addCarriageReturn();
	    r2.setText("结束时间" + "      " + "2013-05-28 13:20");
	    r2.addCarriageReturn();
	    //r2.setBold(true);
	    
	    // 存放试题信息
	    XWPFParagraph p3 = doc.createParagraph();
	    p3.setWordWrap(true);
	    XWPFRun r3 = p3.createRun();
	    r3.setTextPosition(10);
	    r3.setFontSize(15);
	    r3.setText("一、选择题(共50分)");
	    // 题目和选项
	    XWPFParagraph p4 = doc.createParagraph();
	    p4.setWordWrap(true);
	    XWPFRun r4 = p4.createRun();
	    r4.setTextPosition(13);
	    r4.setText("    1、下面说法正确的是?(3分)");
	    r4.addCarriageReturn();
	    r4.setText("        A:子类如果使用父类的方法必须使用super关键字");
	    r4.addCarriageReturn();
	    r4.setText("        B:子类如果使用父类的方法必须使用super关键字");
	    r4.addCarriageReturn();
	    r4.setText("        C:子类如果使用父类的方法必须使用super关键字");
	    r4.addCarriageReturn();
	    r4.setText("        D:子类如果使用父类的方法必须使用super关键字");
	    r4.addCarriageReturn();
	    r4.setText("正确答案:ABCD");
	    r4.setText("选择答案:AC");
	    
	    // 判断题
	    XWPFParagraph p5 = doc.createParagraph();
	    p5.setWordWrap(true);
	    XWPFRun r5 = p5.createRun();
	    r5.setTextPosition(10);
	    r5.setFontSize(15);
	    r5.setText("一、判断题(共50分)");
	    XWPFParagraph p6 = doc.createParagraph();
	    p6.setWordWrap(true);
	    // 题目
	    int i;
	    for (i = 0; i < 5; i++) {
	      XWPFRun r6 = p6.createRun();
	      r6.setTextPosition(13);
	      r6.setText("1、子类如果使用父类的方法必须使用super关键字(5分)");
	      r6.addCarriageReturn();
	      r6.setText("正确答案:对");
	      r6.setText("      ");
	      r6.setSubscript(VerticalAlign.BASELINE);
	      r6.setText("选择答案:");
	      XWPFRun r7 = p6.createRun();
	      r7.setTextPosition(13);
	      // 控制某一个字体颜色为蓝色
	      if (i == 3) {
	        r7.setColor("0A4090");
	        r7.setBold(true);
	      }
	      r7.setText("错");
	      r7.addCarriageReturn();
	    }
	    FileOutputStream out = new FileOutputStream("D:\\test\\simple.docx");
	    doc.write(out);
	    out.close();
	    System.out.println("success");
	  }
	}



 
</pre><pre name="code" class="java">
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值