Java excel转化为pdf(数据写入excel)
String IDs = (String) params.get("IDs");
String[] idList = IDs.split(",");
List<BasicInformation> basic = new ArrayList<BasicInformation>();
for(int i = 0;i<idList.length;i++) {
Integer id = Integer.valueOf(idList[i]);
BasicInformation basicInformation = biddingInformationDao.getClaimInfo(id);//根据basicInformation的id获取信息
basic.add(basicInformation);
}
String fileName = WriteExcel.basicExcel(basic,request);
import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import com.sinosoft.cms.entity.ApplicantInformation;
import com.sinosoft.cms.entity.BasicInformation;
import com.sinosoft.cms.entity.BidInformation;
import com.sinosoft.cms.entity.InsuredInformation;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public static String basicExcel(List<BasicInformation> basic,HttpServletRequest request) {
// 开始写入excel,创建模型文件头
String[] titleA = { "项目名称", "招标人名称","招标人联系电话", "招标方名称", "投保人名称","金额(保费)"};
Date date = new Date();
String str = "yyyMMddHHmmss";
SimpleDateFormat sdf = new SimpleDateFormat(str);
String name = sdf.format(date) + ".xls";
String target = sdf.format(date) + ".pdf";
String pathStr = request.getSession().getServletContext().getRealPath("/");
pathStr = pathStr.replaceAll("\\\\", "/");
System.out.println("系统根目录=="+pathStr);
String url = pathStr+"downloadFile/" + name ;
// 创建Excel文件,B库CD表文件
File fileA = new File(url);
if (fileA.exists()) {
//如果文件存在就删除
fileA.delete();
}
try {
fileA.createNewFile();
// 创建工作簿
WritableWorkbook workbookA = Workbook.createWorkbook(fileA);
// 创建sheet
WritableSheet sheetA = workbookA.createSheet("sheet1", 0);
Label labelA = null;
// 设置列名
for (int i = 0; i < titleA.length; i++) {
labelA = new Label(i, 0, titleA[i]);
sheetA.addCell(labelA);
}
if (basic != null && basic.size() > 0) {
SimpleDateFormat df = new SimpleDateFormat("yyy-MM-dd");
BasicInformation basicInformation = new BasicInformation();
// 获取数据源
for (int i = 1; i - 1 < basic.size(); i++) {
basicInformation = basic.get(i - 1);
BidInformation bidInformation = basicInformation.getBidInformation();//项目名称
InsuredInformation InsuredInformation = basicInformation.getInsuredInformation();//招标人名称
ApplicantInformation applicantInformation = basicInformation.getApplicantInformation();//投标单位名称
labelA = new Label(0, i, bidInformation.getProjectName());
sheetA.addCell(labelA);
labelA = new Label(1, i, InsuredInformation.getInsuredName());
sheetA.addCell(labelA);
labelA = new Label(2, i, InsuredInformation.getInsuredMobile());
sheetA.addCell(labelA);
labelA = new Label(3, i, bidInformation.getBidderName());
sheetA.addCell(labelA);
labelA = new Label(4, i, applicantInformation.getApplicantName());
sheetA.addCell(labelA);
BigDecimal amount = basicInformation.getPermium();//金额(保费)
labelA = new Label(5, i, amount.stripTrailingZeros().toPlainString());
sheetA.addCell(labelA);
}
}
workbookA.write();// 写入数据
workbookA.close();// 关闭连接
String source2 = url;
String target2 = pathStr+"downloadFile/" + target ;
ExcelToPdf.excel2pdf(source2,target2);
System.out.println("成功写入文件,下载地址== " + url);
return target;
} catch (Exception e) {
e.printStackTrace();
System.out.println("文件写入失败,报异常..."+e.toString());
return null;
}
}
//excel 转 pdf 工具类
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class ExcelToPdf {
public static void excel2pdf(String source, String target) {
System.out.println("启动Excel");
long start = System.currentTimeMillis();
ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动excel(Excel.Application)
try {
app.setProperty("Visible", false);
Dispatch workbooks = app.getProperty("Workbooks").toDispatch();
System.out.println("打开文档" + source);
Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[]{source, new Variant(false),new Variant(false)}, new int[3]).toDispatch();
Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] {
target, new Variant(57), new Variant(false),
new Variant(57), new Variant(57), new Variant(false),
new Variant(true), new Variant(57), new Variant(true),
new Variant(true), new Variant(true) }, new int[1]);
Variant f = new Variant(false);
System.out.println("转换文档到PDF " + target);
Dispatch.call(workbook, "Close", f);
long end = System.currentTimeMillis();
System.out.println("转换完成..用时:" + (end - start) + "ms.");
} catch (Exception e) {
System.out.println("========Error:文档转换失败:" + e.getMessage());
}finally {
if (app != null){
app.invoke("Quit", new Variant[] {});
}
}
}
}
public static void main(String[] args){
String source2 = "D:\\abc.xls";
String target2 = "D:\\def.pdf";
ExcelToPdf.excel2pdf(source2,target2);
}
jacob.jar
需要的包来自 https://blog.csdn.net/ordinaryprogrammerc/article/details/84141472
链接:https://pan.baidu.com/s/1n_FDqcQLoBUYWDKLn2ibOw
提取码:62yt
链接:https://pan.baidu.com/s/1uT2bW1GfW_f_eQ4TUva5rg
提取码:4kcw
1.将jacob-1.17-M2-x64.dll 放到java安装目录下: jdk1.X.X\jre\bin,即你的JDK安装路径下的jre中的bin中;
2.将jacob-1.17-M2-x64.dll放到tomcat的bin下面;
3.将jacob-1.17-M2-x64.dll 放到C:\Windows\System32
接着将jacob.jar文件放在eclipse中项目的lib文件夹下,具体为找到项目的目录,进入web-inf目录下的lib目录下,将jacob.jar放进去;