首先创建用例
例如:
运行代码:
预期结果:
实际结果:与预期结果不符合,哪位大佬有代码传授一下啊,实在是不知道咋写了
代码:
package com.znzdh.qitagongju;
import com.spire.doc.*;
import com.spire.doc.documents.VerticalAlignment;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import com.spire.doc.documents.DocumentObjectType;
import com.spire.doc.documents.Paragraph;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Scanner;
public class Eworld {
public static void main(String[] args) {
try{
//urlexcel
String path="D:\\桌面\\Eworld1.xls";
//urlworld
String pathworld="D:\\桌面\\E1.docx";
//解析路径
Workbook workbook=Workbook.getWorkbook(new File(path));
//输入流
//FileOutputStream fos = new FileOutputStream(pathworld);
//创建Document对象,创建word文档
Document doc = new Document();
//获取第一张表
Sheet sheet = workbook.getSheet(0);
//循环获取第一行数据,因为默认第一行为标题行,我们可以从1开始循环,如果需要读取标题行,从0开始
//sheet.getRows(),获取总数行
for (int i = 1; i <sheet.getRows() ; i++) {
//获取第一行的第i行信息 sheet.getcell(列,行);下标从0开始
String name = sheet.getCell(0, i).getContents();
System.out.println("name:"+name);
//获取第二行的第i行信息
String miaoshu = sheet.getCell(1,i).getContents();
System.out.println("miaoshu:"+miaoshu);
//获取第三行的第i行信息
String buzhou = sheet.getCell(2,i).getContents();
System.out.println("buzhou:"+buzhou);
//获取第四行的第i行信息
String yuqi = sheet.getCell(3,i).getContents();
System.out.println("yuqi:"+yuqi);
//fos.write((name+"\n").getBytes());
String[] data={name};
String[] header={miaoshu,buzhou,yuqi};
Section sec = doc.addSection();
//添加表格
Table table2 = sec.addTable(true);
table2.resetCells(1, 1);
Table table = sec.addTable(true);
table.resetCells(1, 3);
//设置表格第一行作为表头,写入表头数组内容,并格式化表头数据
TableRow row = table.getRows().get(0);
TableRow row2 = table2.getRows().get(0);
for (int t = 0; t < data.length; t++) {
row2.getCells().get(t).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
Paragraph p = row2.getCells().get(t).addParagraph();
p.appendText(name+":");
for (int j = 0; j < header.length; j++) {
row.getCells().get(j).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
Paragraph k = row.getCells().get(j).addParagraph();
k.appendText(header[j]);
//保存文档
doc.saveToFile(pathworld, FileFormat.Docx_2013);
}
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}