package org.htz.core;
import java.awt.Color;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.htz.pdfevent.PageEvent;
import jxl.Cell;
import jxl.CellType;
import jxl.Range;
import jxl.Sheet;
import jxl.format.Alignment;
import jxl.format.BoldStyle;
import jxl.format.BorderLineStyle;
import jxl.format.CellFormat;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
public class Convert extends WriterPdf{
/**pdf表格*/
private Table table=null;
/** 当 Excel 的 border 是 NONE 是,pdf 的 border 是否是 0 */
private boolean noEmptyBorder = false;
/**工作簿数组*/
private Sheet[] sheets=null;
/**工作簿*/
private Excel excel=null;
/**页眉*/
private HeaderText header;
/**最后一个工作簿图片*/
private SheetImage sheetImage;
int iRows = 0;
int iTotalrows = 0;
int iCount = 0;
/**获得页面对象*/
public HeaderText getHeader() {
return header;
}
/**
* 读入一个excel文件获得一个pdf文件输出流
* @param filePath excel文件路径
* @param output pdf输出流
*/
public Convert(String filePath,OutputStream output) {
super(output);
readExcel(filePath);
}
/**
* 将指定路径一个excel文件转化为指定路径的pdf文件
* @param filePath excel文件路径
* @param destFilePath pdf文件路径
*/
public Convert(String filePath,String destFilePath){
super(destFilePath);
readExcel(filePath);
}
/**
* 从数据库中读入一个excel文件的输入转化为一个输出流
* @param input excel文件的输入流
* @param output excel文件输出流
*/
public Convert(InputStream input,OutputStream output){
super(output);
readExcel(input);
}
/**
* 读取excel的方法
* @param obj Object对象
*/
private void readExcel(Object obj){
excel=new Excel();
if(obj instanceof String){
String filePath=(String)obj;
excel.readExcel(filePath);//从输入流中读入excel文件
}else if(obj instanceof InputStream ){
InputStream input=(InputStream)obj;
excel.readExcelFromDB(input);
}
sheets=excel.getSheets();//获得excel文件工作簿数
for(int i=0;i<=sheets.length-1;i++)
{
int rows=sheets[i].getRows();
if(rows != 0)
{
this.setITotalrows(rows);
}
}
System.out.println("->sheet的个数为:"+sheets.length);
int length=sheets.length-1;
ExcelSheet image_Sheet=new ExcelSheet(sheets[length]);
List imageList=image_Sheet.getImages();
sheetImage=new SheetImage();
sheetImage.setImage(imageList);
try {
header=readHeader(sheets[length]);
} catch (Exception e) {
System.err.println("->读取页眉出错");
e.printStackTrace();
}
}
/**
* 转化的核心方法
* @param pageEvent 页面事件
* @throws Exception
*/
public void convert(PageEvent pageEvent)throws Exception{
noEmptyBorder = true;
writer.setPageEvent(pageEvent);//设置页面事件
document.setPageSize(pageSize);//设置pdf页面的大小
document.open();//打开document对象
System.out.println("->document已经打开");
if(sheets==null||sheets.length<0){
System.out.println("->excel文件中没有工作簿");
return;
}
for(int i=0;i<=sheets.length-1;i++){
int colswidth[]=colsWidth(sheets[i]);
int rows=sheets[i].getRows();
int cols=sheets[i].getColumns();
Map merge=new HashMap();//存放合并单元格的左上角的坐标和在list中的下标位置
List subs=new ArrayList();//存放不包括左上角下标位置的已合并单元格的位置
Range[] range=sheets[i].getMergedCells();//得到工作簿的合并单元格的数组
for(int k=0;k<range.length;k++){
int row=range[k].getTopLeft().getRow();
int col=range[k].getTopLeft().getColumn();
int index=row*cols+col+1;
int r=range[k].getBottomRight().getRow()-range[k].getTopLeft().getRow()+1;
int c=range[k].getBottomRight().getColumn()-range[k].getTopLeft().getColumn()+1;
for(int m=row;m<=range[k].getBottomRight().getRow();m++){
for(int n=col;n<=range[k].getBottomRight().getColumn();n++){
if(m!=row||n!=col){
int xy=m*cols+n+1;
subs.add(Integer.valueOf(xy));
}
}
}
String key=r+","+c;
merge.put(Integer.valueOf(index),key);
}
if(cols>0){
List cells=new ArrayList();
table=new Table(cols);//创建含有cols列的表格
table.setWidths(colswidth);//设置每列的大小
table.setPadding(2.0f);//设置填充间隔
table.setSpacing(0.0f);//设置单元格之间距离
table.setWidth(100.0f);//设置表格的宽度百分比
table.setBorder(0);//设置表格的边框
table.setOffset(30.0f);//设置表与表之间的偏移量
cells=getSheetCell(sheets[i],rows,cols);
for(int p=0;p<cells.size();p++){
if(!findIndex(p,subs)){
boolean mergerow=false;
if(merge.containsKey(Integer.valueOf(p+1))){
mergerow=true;
com.lowagie.text.Cell pcell=null;
Phrase phrase = null;
Cell jxlcell=(Cell) cells.get(p);
CellFormat format = jxlcell.getCellFormat();//取得单元格的格式
Font font = null;
if(format != null && format.getFont() != null) {
font = convertFont(format.getFont());// 调用convertFont()的方法转变字体
}else{
font = new Font(Font.COURIER, 10.0f, Font.NORMAL, Color.BLACK);
}
String content=jxlcell.getContents();
String key=(String) merge.get(Integer.valueOf(p+10));
String []s=key.split(",");
int r=Integer.parseInt(s[0]);//合并单元格左上角的行数
int c=Integer.parseInt(s[1]);//合并单元格左上角的列数
phrase=new Phrase(content,font);
pcell=new com.lowagie.text.Cell();
pcell.addElement(phrase);
if(r>1){
pcell.setRowspan(r);//对行进行合并
}
if(c>1){
pcell.setColspan(c);//对列进行合并
}
transferFormat(pcell, jxlcell,p,cols,cells);//将jxl中的cell转化为pdf的cell
pcell.setBorderWidthRight(2.0f);
table.addCell(pcell);
}else{
com.lowagie.text.Cell pcell=null;
Phrase phrase = null;
Cell jxlcell=(Cell) cells.get(p);
CellFormat format = jxlcell.getCellFormat();//取得单元格的格式
Font font = null;
if(format != null && format.getFont() != null) {
font = convertFont(format.getFont());// 调用convertFont()的方法转变字体
}else{
font = new Font(Font.COURIER, 10.0f, Font.NORMAL, Color.BLACK);
}
String content=jxlcell.getContents();
phrase=new Phrase(content,font);
pcell=new com.lowagie.text.Cell();
pcell.addElement(phrase);
transferFormat(pcell, jxlcell,p,cols,cells);//将jxl中的cell转化为pdf的cell
table.addCell(pcell);
// }
}
}
}
System.out.println(table);
document.add(table);//将table对象添加到文档对象中去
cells.clear();//清空cell单元格
}
}
System.out.println("执行了");
if(iCount == this.getITotalrows())
{
CloseDocument();
excel.closeWorkbook();
}
}
/**
* 获得工作簿中所有列宽的数组
* @param sheet 工作簿
* @return 列宽数组
*/
private int[] colsWidth(Sheet sheet){
int width[]=new int[sheet.getColumns()];
for(int i=0;i<width.length;i++){
width[i]=sheet.getColumnView(i).getSize();
}
return width;
}
/**
* 将当前工作簿中的所有单元格添加到List集合中
* @param sheet 工作簿
* @param rows 工作簿行数
* @param cols 工作簿列数
*/
private List getSheetCell(Sheet sheet,int rows,int cols){
List cells=new ArrayList();
int i = 0;
if(iRows != 0)
{
iRows++;
}
for(i = iRows;i<rows;i++){
for(int j=0;j<cols;j++){
System.out.p
JXL+IText把excel转换为PDF
4星 · 超过85%的资源 需积分: 12 134 浏览量
2011-06-26
13:52:59
上传
评论 1
收藏 2.8MB RAR 举报
hantaozuvip
- 粉丝: 75
- 资源: 11
最新资源
- 什么是移动应用开发-关于移动应用开发的相关介绍
- 第二组汇报 PPT.pptx
- 什么是大数据开发-相关介绍-关于大数据开发的相关介绍
- 什么是软件测试-相关介绍-关于软件测试的相关介绍
- 前端开发-什么是前端开发-关于前端开发的一些相关介绍
- Sora AI-关于文生视频的使用场景说明
- suno AI文生视频的相关教程和介绍使用
- 什么是后端开发-关于后端开发的一些小介绍分享
- Jurassic Pack Vol. II Dinosaurs 侏罗纪包卷恐龙二号Unity游戏模型资源unitypackage
- Jurassic Pack Vol. III Dinosaurs 侏罗纪包卷恐龙三号Unity游戏模型资源unitypackag
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页