【Java】iText POI
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/";@Testpublic 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 -> ListItemprivate 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;}}
【Java】iText POI相关推荐
- 【java】反射+poi 导出excel
2019独角兽企业重金招聘Python工程师标准>>> 反射 导出的数组转变成对象 private static Object expexcelMaptobean(Class< ...
- 【Java】eclipse如何导入项目
[Java]eclipse如何导入项目 1.第一步,打开eclipse,点击file->import 2.第二步,选择general->existing projects into wor ...
- 【Java】Java连接Mysql数据库的demo示例
[Java]Java连接Mysql数据库的demo示例 1.安装mysql数据库 2.下载java-mysql-connector.jar包 3.完成java配置 4.写java代码运行测试 1.安装 ...
- 【Java】泛型中 extends 和 super 的区别?
<? extends T>和<? super T>是Java泛型中的"通配符(Wildcards)"和"边界(Bounds)"的概念. ...
- java反射invoke空指针_【Java】Java 反射 object is not an instance of declaring cla
[Java]Java 反射 object is not an instance of declaring cla [Java]Java 反射 object is not an instance of ...
- synchronized【Java】中使用的demo
synchronized[Java]中使用的demo 没有synchronized的效果: package Action;public class syn {static int count=10;s ...
- 【Java】NIO中Selector的select方法源码分析
该篇博客的有些内容和在之前介绍过了,在这里再次涉及到的就不详细说了,如果有不理解请看[Java]NIO中Channel的注册源码分析, [Java]NIO中Selector的创建源码分析 Select ...
- LeetCode题库整理【Java】—— 3 无重复字符的最长子串
LeetCode题库整理[Java] ## 3 无重复字符的最长子串 题目:给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" ...
- LeetCode题库整理【Java】—— 2 两数相加
LeetCode题库整理[Java] 2.两数相加 题目:给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果, ...
最新文章
- oracle中merge的用法,以及各版本的区别 Create checkbox全选JS(转载)
- VS生成Cordova for Android应用之Gradle
- 如何使用ListView实现一个带有网络请求,解析,分页,缓存的公共的List页面来大大的提高工作效率
- c语言 个位,如何才能给C语言增加几个位操作函数
- git-分支管理-增加删除切换合并操作
- Nginx实现HTTP反向代理配置
- java ext pagesize_更改透明图像的不透明度/更改extgstate字典的值
- ECCV 2020 | 空间-角度信息交互的光场图像超分辨,性能优异代码已开源
- 设计模式之--单例模式
- 助力数字化运营:商超自动抓单系统
- 基于 Apache Mahout 构建社会化推荐引擎
- 在html中实现word中打批注的功能
- Java五子棋全代码
- Python实现二维码扫码登录
- 在React项目中引入字体文件并使用
- 学习《软件工程》心得
- 人工神经网络连接权重的优化与调整
- Python-函数入参和全局变量
- Docker容器之harbor私有仓库部署与管理
- Mac 脚本之applescript