用java将数据导出到wps表格中 在数据库里面存了一份wps文档

经验直达:

  • 在数据库里面存了一份wps文档
  • 用java将数据导出到wps表格中
  • java能否wps调用页码

一、在数据库里面存了一份wps文档


String fgbh = request.getParameter("fgbh");
DynaBean bean = this.queryByFgbh(fgbh);
File file = new File(getSaveBasePath() File.separator BeanUtils.getString(bean,"WJMC"));
//设置文件头
//response.setContentType("application/msword;charset=UTF-8");
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Content-Disposition","attachment;filename=" new String(BeanUtils.getString(bean,"FGMC").getBytes("GBK"),"ISO8859-1") """);
//提取数据库中的文件
ServletOutputStream sout = response.getOutputStream();
FileUtils.putFileToStream(file.getAbsolutePath(),sout);
sout.flush();
sout.close();
out.clear();
out=pageContext.pushBody();

你可以试试这段代码...


二、用java将数据导出到wps表格中


一:页面图片显示预览:
1)如下图:
用java将数据导出到wps表格中 在数据库里面存了一份wps文档



2)点击导出按钮后预览:
用java将数据导出到wps表格中 在数据库里面存了一份wps文档

3)最终生成的excel表格图片预览:
用java将数据导出到wps表格中 在数据库里面存了一份wps文档

二:代码演示:
说明:执行操作时,请先引进导出excel表格的jar文件包 。
找到导出按钮所执行的js方法,在java后天查看该方法的实现即可 。
1)jsp代码:


[html] view plaincopyprint?


驾校合格率排名




驾校合格率排名

统计日期:







名次名称科目一科目二科目三平均合格率操作%hgltj.action?method=getTbForJxxh&tjjxkshgl.jxxh=&tjjxkshgl.zt=1','',1250,750);">图表




2)java代码演示:




[java] view plaincopyprint?
/**
* 驾校合格率导出excel图表
*/
//response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
String tmptitle = "驾校合格率排名"; // 标题
response.setHeader("Content-disposition", "attachment; filename=" new String(tmptitle.getBytes(),"iso8859-1") ".xls");// 设定输出文件头
response.setContentType("application/vnd.ms-excel");// 定义输出类型
wbook = Workbook.createWorkbook(os); // 建立excel文件
WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
// 设置excel标题
//cellFormat.setBackground(Colour.AQUA);
cellFormat.setFont(wfont);
label.setCellFormat(cellFormat);
wsheet.addCell(label);
//wsheet.addCell(new Label(0, 0, tmptitle, wcfFC));
wsheet.setRowView(0,500); //第一行高度
wsheet.mergeCells(0, 0, 6, 1);//合并单元格(第一列的第一行和第七列的第二行合并)
//wsheet.mergeCells(0, 1, 9, 1);
//wsheet.mergeCells(0, 2, 0, 4);
//wsheet.mergeCells(1, 2, 3, 2);
//wsheet.mergeCells(4, 2, 6, 2);
//wsheet.mergeCells(7, 2, 9, 2);
wsheet.setColumnView(0,10); //宽度
wsheet.setColumnView(1,25); //宽度
wsheet.setColumnView(2,10); //宽度
wsheet.setColumnView(3,10); //宽度
wsheet.setColumnView(4,10); //宽度
wsheet.setColumnView(5,10); //宽度
// 开始生成主体内容
wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
wcfFC = new WritableCellFormat(wfont);
wsheet.addCell(new Label(0, 2, "名次",wcfFC));
wsheet.addCell(new Label(1, 2, "驾校名称",wcfFC));
wsheet.addCell(new Label(2, 2, "科目一",wcfFC));
wsheet.addCell(new Label(3, 2, "科目二",wcfFC));
wsheet.addCell(new Label(4, 2, "科目三",wcfFC));
wsheet.addCell(new Label(5, 2, "合格率",wcfFC));
int count=jxhelpmList.size();
if(count>0){////判断集合是否不为0
TjJxkshgl tjhgl=null;
for(int i=0;i
tjhgl=(TjJxkshgl)jxhelpmList.get(i);
wsheet.addCell(new Label(0, i 3, (i 1) ""));
wsheet.addCell(new Label(1, i 3, tjhgl.getJxmc()));
wsheet.addCell(new Label(2, i 3, tjhgl.getKm1hgl()));
wsheet.addCell(new Label(3, i 3, tjhgl.getKm2hgl()));
wsheet.addCell(new Label(4, i 3, tjhgl.getKm3hgl()));
wsheet.addCell(new Label(5, i 3, tjhgl.getAvghgl()));


}


三、java能否wps调用页码


【用java将数据导出到wps表格中 在数据库里面存了一份wps文档】1. [代码][Java]代码
package experiments;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.DispatchEvents;
import com.jacob.com.Variant;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Doc2Pdf {

public static Converter newConverter(String name) {
if (name.equals("wps")) {
return new Wps();
} else if (name.equals("pdfcreator")) {
return new PdfCreator();
}
return null;
}

public synchronized static boolean convert(String word, String pdf) {
return newConverter("pdfcreator").convert(word, pdf);
}

public static interface Converter {

public boolean convert(String word, String pdf);
}

public static class Wps implements Converter {

public synchronized boolean convert(String word, String pdf) {
File pdfFile = new File(pdf);
File wordFile = new File(word);
ActiveXComponent wps = null;
try {
wps = new ActiveXComponent("wps.application");
ActiveXComponent doc = wps.invokeGetComponent("Documents").invokeGetComponent("Open", new Variant(wordFile.getAbsolutePath()));
doc.invoke("ExportPdf", new Variant(pdfFile.getAbsolutePath()));
doc.invoke("Close");
doc.safeRelease();
} catch (Exception ex) {
Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
return false;
} catch (Error ex) {
Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
return false;
} finally {
if (wps != null) {
wps.invoke("Terminate");
wps.safeRelease();
}
}
return true;
}
}

public static class PdfCreator implements Converter {

public static final int STATUS_IN_PROGRESS = 2;
public static final int STATUS_WITH_ERRORS = 1;
public static final int STATUS_READY = 0;
private ActiveXComponent pdfCreator;
private DispatchEvents dispatcher;
private volatile int status;
private Variant defaultPrinter;

private void init() {
pdfCreator = new ActiveXComponent("PDFCreator.clsPDFCreator");
dispatcher = new DispatchEvents(pdfCreator, this);
pdfCreator.setProperty("cVisible", new Variant(false));
pdfCreator.invoke("cStart", new Variant[]{new Variant("/NoProcessingAtStartup"), new Variant(true)});
setCOption("UseAutosave", 1);
setCOption("UseAutosaveDirectory", 1);
setCOption("AutosaveFormat", 0);// 0 = PDF
defaultPrinter = pdfCreator.getProperty("cDefaultPrinter");
status = STATUS_IN_PROGRESS;
pdfCreator.setProperty("cDefaultprinter", "PDFCreator");
pdfCreator.invoke("cClearCache");
pdfCreator.setProperty("cPrinterStop", false);
}

private void setCOption(String property, Object value) {
Dispatch.invoke(pdfCreator, "cOption", Dispatch.Put, new Object[]{property, value}, new int[2]);
}

private void close() {
if (pdfCreator != null) {
pdfCreator.setProperty("cDefaultprinter", defaultPrinter);
pdfCreator.invoke("cClearCache");
pdfCreator.setProperty("cPrinterStop", true);
pdfCreator.invoke("cClose");
pdfCreator.safeRelease();
pdfCreator = null;
}
if (dispatcher != null) {
dispatcher.safeRelease();
dispatcher = null;
}
}

public synchronized boolean convert(String word, String pdf) {
File pdfFile = new File(pdf);
File wordFile = new File(word);
try {
init();
setCOption("AutosaveDirectory", pdfFile.getParentFile().getAbsolutePath());
if (pdfFile.exists()) {
pdfFile.delete();
}
pdfCreator.invoke("cPrintfile", wordFile.getAbsolutePath());
int seconds = 0;
while (isInProcess()) {
seconds;
if (seconds > 30) { // timeout
throw new Exception("convertion timeout!");
}
Thread.sleep(1000);
}
if (isWithErrors()) return false;
// 由于转换前设置cOption的AutosaveFilename不能保证输出的文件名与设置的相同(pdfcreator会加入/修改后缀名)
// 所以这里让pdfcreator使用自动生成的文件名进行输出,然后在保存后将其重命名为目标文件名
File outputFile = new File(pdfCreator.getPropertyAsString("cOutputFilename"));
if (outputFile.exists()) {
outputFile.renameTo(pdfFile);
}
} catch (InterruptedException ex) {
Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
return false;
} catch (Exception ex) {
Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
return false;
} catch (Error ex) {
Logger.getLogger(Doc2Pdf.class.getName()).log(Level.SEVERE, null, ex);
return false;
} finally {
close();
}
return true;
}

private boolean isInProcess() {
return status == STATUS_IN_PROGRESS;
}

private boolean isWithErrors() {
return status == STATUS_WITH_ERRORS;
}

// eReady event
public void eReady(Variant[] args) {
status = STATUS_READY;
}

// eError event
public void eError(Variant[] args) {
status = STATUS_WITH_ERRORS;
}
}

public static void main(String[] args) {
convert("e:\test.doc", "e:\output.pdf");
}
}

相关经验推荐