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相关推荐

  1. 【java】反射+poi 导出excel

    2019独角兽企业重金招聘Python工程师标准>>> 反射 导出的数组转变成对象 private static Object expexcelMaptobean(Class< ...

  2. 【Java】eclipse如何导入项目

    [Java]eclipse如何导入项目 1.第一步,打开eclipse,点击file->import 2.第二步,选择general->existing projects into wor ...

  3. 【Java】Java连接Mysql数据库的demo示例

    [Java]Java连接Mysql数据库的demo示例 1.安装mysql数据库 2.下载java-mysql-connector.jar包 3.完成java配置 4.写java代码运行测试 1.安装 ...

  4. 【Java】泛型中 extends 和 super 的区别?

    <? extends T>和<? super T>是Java泛型中的"通配符(Wildcards)"和"边界(Bounds)"的概念. ...

  5. 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 ...

  6. synchronized【Java】中使用的demo

    synchronized[Java]中使用的demo 没有synchronized的效果: package Action;public class syn {static int count=10;s ...

  7. 【Java】NIO中Selector的select方法源码分析

    该篇博客的有些内容和在之前介绍过了,在这里再次涉及到的就不详细说了,如果有不理解请看[Java]NIO中Channel的注册源码分析, [Java]NIO中Selector的创建源码分析 Select ...

  8. LeetCode题库整理【Java】—— 3 无重复字符的最长子串

    LeetCode题库整理[Java] ## 3 无重复字符的最长子串 题目:给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" ...

  9. LeetCode题库整理【Java】—— 2 两数相加

    LeetCode题库整理[Java] 2.两数相加 题目:给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果, ...

最新文章

  1. oracle中merge的用法,以及各版本的区别 Create checkbox全选JS(转载)
  2. VS生成Cordova for Android应用之Gradle
  3. 如何使用ListView实现一个带有网络请求,解析,分页,缓存的公共的List页面来大大的提高工作效率
  4. c语言 个位,如何才能给C语言增加几个位操作函数
  5. git-分支管理-增加删除切换合并操作
  6. Nginx实现HTTP反向代理配置
  7. java ext pagesize_更改透明图像的不透明度/更改extgstate字典的值
  8. ECCV 2020 | 空间-角度信息交互的光场图像超分辨,性能优异代码已开源
  9. 设计模式之--单例模式
  10. 助力数字化运营:商超自动抓单系统
  11. 基于 Apache Mahout 构建社会化推荐引擎
  12. 在html中实现word中打批注的功能
  13. Java五子棋全代码
  14. Python实现二维码扫码登录
  15. 在React项目中引入字体文件并使用
  16. 学习《软件工程》心得
  17. 人工神经网络连接权重的优化与调整
  18. Python-函数入参和全局变量
  19. Docker容器之harbor私有仓库部署与管理
  20. Mac 脚本之applescript

热门文章

  1. python制表符长度不_python \t python里的 \t 的长度具体是几个字符?
  2. WIFI宝—— 源自WIFI共享精灵的味道
  3. 局域网怎么查看单位摄像头_一份家用户外监控摄像头的选购和安装记录
  4. 库克「豪赌」MR:七年磨一剑,不行也得行
  5. 推荐|掌握这12 条经验,才算学懂了机器学习
  6. component使用
  7. 黄山为什么不属于五岳?
  8. Java web医院门诊挂号系统(适合初学者)
  9. QQ和360替代方案。
  10. thinkPHP 批量删除