iText是一個可以生成PDF文檔的工具。POI可以用來讀取Word、Excel文檔。最近由於在做項目,所以很久沒更新博客了,這次博客更新是項目中使用的小demo,希望可以幫助到需要的人。 --來了,小老弟
iText
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
×這裏需要將你所用到的字體資源放在resources下面即可。
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;
public class PDFTools {
private static final String FILE_LOCATION = "/home/huangwei/Desktop/";
@Test
public void test() {
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE_LOCATION + "test.pdf"));
this.genPaper(document);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// Document(Title, Author, Password) -> Paragraph -> List -> ListItem
private void genPaper(Document document) {
/*初始化楷體和黑體兩種字體*/
Font kaiFont = null;
Font heiFont = null;
try {
BaseFont baseFont1 = BaseFont.createFont("simkai.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
BaseFont baseFont2 = BaseFont.createFont("simhei.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
kaiFont = new Font(baseFont1);
heiFont = new Font(baseFont2);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
document.open();
try {
/*生成選擇題*/
Paragraph selection = new Paragraph("一.選擇題(共30分,每道題3分)", heiFont);
List list = new List(List.ORDERED);
for (int i = 0; i < 10; i++) {
ListItem question = new ListItem("在JSP中,哪個指令用來聲明JSP欲應用的標籤庫?( )\n" +
"A.tld B.page c.import D.taglib", kaiFont);
list.add(question);
}
selection.add(list);
document.add(selection);
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
POI的使用
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
import com.lanou.entity.pojo.TbOnline;
import com.lanou.entity.pojo.TbSelection;
import com.lanou.entity.pojo.TbSubjection;
import com.lanou.util.GException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class IPOITools {
private static final int E=9;
private static final int F=10;
public static List<TbSelection> uploadSelection(File file) throws GException {
List<TbSelection> questions = new ArrayList<TbSelection>();
Workbook workbook = null;
try {
workbook = initTools(file);
} catch (IOException e) {
throw new GException("POI工具IO異常");
}
if (null == workbook) {
return null;
}
//將文件內容同步到List<TbSelection> questions中
return syncSelection(workbook,questions);
}
public static List<TbOnline> uploadOnline(File file) throws GException {
List<TbOnline> questions = new ArrayList<TbOnline>();
Workbook workbook = null;
try {
workbook = initTools(file);
} catch (IOException e) {
throw new GException("POI工具IO異常");
}
if (null == workbook) {
return null;
}
return syncOnline(workbook,questions);
}
public static List<TbSubjection> uploadSubjection(File file) throws GException {
List<TbSubjection> questions = new ArrayList<TbSubjection>();
Workbook workbook = null;
try {
workbook = initTools(file);
} catch (IOException e) {
throw new GException("POI工具IO異常");
}
if (null == workbook) {
return null;
}
return syncSubjection(workbook,questions);
}
/*以下都是工具*/
private static Workbook initTools(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
Workbook workbook = new HSSFWorkbook(fis);
return workbook;
}
private static List<TbSelection> syncSelection(Workbook workbook,List<TbSelection> questions) {
//拿到工作表
Sheet sheet = workbook.getSheet("sheet1");
//去掉首行
sheet.removeRow(sheet.getRow(0));
for (Row row : sheet) {
TbSelection selection = new TbSelection();
Cell tmp = row.getCell(0);
if (null == tmp || tmp.getStringCellValue() == "") {
continue;
}
//將excel的內容同步到實體集合
//根據選項E,F的值是否爲空,來判斷當前是否爲單選多選。
selection.setQuestion(row.getCell(0).getStringCellValue());
selection.setAnswer(row.getCell(1).getStringCellValue());
selection.setScore((int)row.getCell(2).getNumericCellValue());
selection.setWeight((byte)row.getCell(3).getNumericCellValue());
selection.setCourse((int)row.getCell(4).getNumericCellValue());
selection.setSelectiona(row.getCell(5).getStringCellValue());
selection.setSelectionb(row.getCell(6).getStringCellValue());
selection.setSelectionc(row.getCell(7).getStringCellValue());
selection.setSelectiond(row.getCell(8).getStringCellValue());
//通過try塊,判斷是否爲多選題,若不是設置爲空
try{
selection.setSelectione(row.getCell(E).getStringCellValue());
selection.setSelectionf(row.getCell(E).getStringCellValue());
}catch (Exception e){
selection.setSelectione("");
selection.setSelectionf("");
}
//通過try塊,判斷是否有notes,若無設置爲空
try{
selection.setNotes(row.getCell(11).getStringCellValue());
}catch (Exception e){
selection.setNotes("");
}
questions.add(selection);
}
return questions;
}
private static List<TbSubjection> syncSubjection(Workbook workbook,List<TbSubjection> questions) {
//拿到工作表
Sheet sheet = workbook.getSheet("sheet1");
//去掉首行
sheet.removeRow(sheet.getRow(0));
for (Row row : sheet) {
TbSubjection subjection = new TbSubjection();
Cell tmp = row.getCell(0);
if (null == tmp || tmp.getStringCellValue() == "") {
continue;
}
//將excel的內容同步到實體集合
subjection.setQuestion(row.getCell(0).getStringCellValue());
subjection.setReferrence(row.getCell(1).getStringCellValue());
subjection.setScore((int)row.getCell(2).getNumericCellValue());
subjection.setWeight((byte)row.getCell(3).getNumericCellValue());
subjection.setCourse((int)row.getCell(4).getNumericCellValue());
try{
subjection.setNotes(row.getCell(5).getStringCellValue());
}catch (Exception e){
subjection.setNotes("");
}
questions.add(subjection);
}
return questions;
}
private static List<TbOnline> syncOnline(Workbook workbook,List<TbOnline> questions) {
//拿到工作表
Sheet sheet = workbook.getSheet("sheet1");
//去掉首行
sheet.removeRow(sheet.getRow(0));
for (Row row : sheet) {
TbOnline online = new TbOnline();
Cell tmp = row.getCell(0);
if (null == tmp || tmp.getStringCellValue() == "") {
continue;
}
//將excel的內容同步到實體集合
online.setQuestion(row.getCell(0).getStringCellValue());
online.setReferrence(row.getCell(1).getStringCellValue());
online.setScore((int)row.getCell(2).getNumericCellValue());
online.setWeight((byte)row.getCell(3).getNumericCellValue());
online.setCourse((int)row.getCell(4).getNumericCellValue());
try{
online.setNotes(row.getCell(5).getStringCellValue());
}catch (Exception e){
online.setNotes("");
}
questions.add(online);
}
return questions;
}
}