poi 打印 word 参考这个 // Setting the property "Name" for the favoured printer (name of // IP address) com.sun.star.beans.PropertyValue propertyValue[] = new com.sun.star.beans.PropertyValue[1]; propertyValue[0] = new com.sun.star.beans.PropertyValue(); propertyValue[0].Name = "Name"; propertyValue[0].Value = args[ 0 ];
// Setting the name of the printer xPrintable.setPrinter( propertyValue );
// Setting the property "Pages" so that only the desired pages // will be printed. propertyValue[0] = new com.sun.star.beans.PropertyValue(); propertyValue[0].Name = "Pages"; propertyValue[0].Value = args[ 2 ];
OutputStream os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader("Content-disposition", "inline; filename=fine.xls");// 设定输出文件头
response.setContentType("application/vnd.ms-excel");// 定义输出类型
Workbook wb = Workbook.getWorkbook(new File(realpath));
WritableWorkbook wbook = Workbook.createWorkbook(os, wb);
// Workbook.c
//第三步:选择模板中名称为StateResult的Sheet:
WritableSheet wsheet = wbook.getSheet("testJXL"); word怎么用response这样输出呢,不要生成一个文件。。
//创建一个集合存放数据,假装是从数据库读取出来的。
List list = new ArrayList();
for(int i=0;i<20;i++){
DealMonito md = new DealMonito();
md.setId(i);
md.setProductName("productName"+i);
md.setProductCode("MS000000"+i);
md.setEnterprise("enterprise"+i);
md.setBuyUserId(i);
md.setSellUserId(i);
list.add(md);
} //设置好输出用什么来输出是Excel还是别的什么东西。
response.reset();
response.setContentType("application/msexcel");
response.setHeader("Content-disposition","inline;filename=Results.xls");
//创建一个Excel
HSSFWorkbook wb = new HSSFWorkbook();
//创建一个页,页的名字叫Sheet1
HSSFSheet sheet = wb.createSheet("sheet1");
//设置单元格的宽度
sheet.setDefaultColumnWidth((short) 20);
//创建字体对象
HSSFFont font = wb.createFont();//加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//字体用什么 font.setFontName("黑体 ");
//字体大小 font.setFontHeight((short)300);
//单元格样式对象
HSSFCellStyle cellStyle = wb.createCellStyle();//单元格的字体用什么?就用上面设置好的东西
cellStyle.setFont(font);//单元格居中显示。
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setWrapText(true);
//行对象创建,行和列都是从0开始的。。
HSSFRow rows = sheet.createRow((short) 0);
rows.setHeightInPoints(20);
//列对象
HSSFCell cell1 = rows.createCell((short) 0);
HSSFCell cell2 = rows.createCell((short) 1);
HSSFCell cell3 = rows.createCell((short) 2);
HSSFCell cell4 = rows.createCell((short) 3);
HSSFCell cell5 = rows.createCell((short) 4);
HSSFCell cell6 = rows.createCell((short) 5);
//设置单元格样式,就用上面的
cell1.setCellStyle(cellStyle);
cell2.setCellStyle(cellStyle);
cell3.setCellStyle(cellStyle);
cell4.setCellStyle(cellStyle);
cell5.setCellStyle(cellStyle);
cell6.setCellStyle(cellStyle);
//设置单元格的值,这个事已经废弃的方法,但是还是可以用的,暂时没有找到替代的方法
cell1.setCellValue("产品名称");
cell2.setCellValue("批准文号");
cell3.setCellValue("生产企业");
cell4.setCellValue("产品数量");
cell5.setCellValue("买方ID");
cell6.setCellValue("卖方ID");
//创建一个新的单元格样式对象,在下面用
HSSFCellStyle cs = wb.createCellStyle();
cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//循环添加元素
for(int i=1;i<list.size();i++){
HSSFRow row = sheet.createRow((short) i);
DealMonito md = (DealMonito)list.get(i);
cell1 = row.createCell((short) 0);
cell2 = row.createCell((short) 1);
cell3 = row.createCell((short) 2);
cell4 = row.createCell((short) 3);
cell5 = row.createCell((short) 4);
//设置单元格样式
cell1.setCellStyle(cs);
cell2.setCellStyle(cs);
cell3.setCellStyle(cs);
cell4.setCellStyle(cs);
cell5.setCellStyle(cs);
cell1.setCellValue(md.getProductName());
cell2.setCellValue(md.getProductCode());
cell3.setCellValue(md.getNumber());
cell4.setCellValue(md.getEnterprise());
cell5.setCellValue(md.getBuyUserId());
}
System.out.println("----------------------");
//关闭相应的流
wb.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();//return 到页面。这样页面就会出来一个保存文件的对话框,这样就算完成导出了,也算作完了打印了,//用户可以自己在调整或者不调整直接打印页OK,因为Java直接打印非常麻烦,通常都会导出到Excel或者World//这样的载体在打印
return mapping.findForward("success");
}
//创建一个集合存放数据,假装是从数据库读取出来的。
List list = new ArrayList();
for(int i=0;i<20;i++){
DealMonito md = new DealMonito();
md.setId(i);
md.setProductName("productName"+i);
md.setProductCode("MS000000"+i);
md.setEnterprise("enterprise"+i);
md.setBuyUserId(i);
md.setSellUserId(i);
list.add(md);
} //设置好输出用什么来输出是Excel还是别的什么东西。
response.reset();
response.setContentType("application/msexcel");
response.setHeader("Content-disposition","inline;filename=Results.xls");
//创建一个Excel
HSSFWorkbook wb = new HSSFWorkbook();
//创建一个页,页的名字叫Sheet1
HSSFSheet sheet = wb.createSheet("sheet1");
//设置单元格的宽度
sheet.setDefaultColumnWidth((short) 20);
//创建字体对象
HSSFFont font = wb.createFont();//加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//字体用什么 font.setFontName("黑体 ");
//字体大小 font.setFontHeight((short)300);
//单元格样式对象
HSSFCellStyle cellStyle = wb.createCellStyle();//单元格的字体用什么?就用上面设置好的东西
cellStyle.setFont(font);//单元格居中显示。
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setWrapText(true);
//行对象创建,行和列都是从0开始的。。
HSSFRow rows = sheet.createRow((short) 0);
rows.setHeightInPoints(20);
//列对象
HSSFCell cell1 = rows.createCell((short) 0);
HSSFCell cell2 = rows.createCell((short) 1);
HSSFCell cell3 = rows.createCell((short) 2);
HSSFCell cell4 = rows.createCell((short) 3);
HSSFCell cell5 = rows.createCell((short) 4);
HSSFCell cell6 = rows.createCell((short) 5);
//设置单元格样式,就用上面的
cell1.setCellStyle(cellStyle);
cell2.setCellStyle(cellStyle);
cell3.setCellStyle(cellStyle);
cell4.setCellStyle(cellStyle);
cell5.setCellStyle(cellStyle);
cell6.setCellStyle(cellStyle);
//设置单元格的值,这个事已经废弃的方法,但是还是可以用的,暂时没有找到替代的方法
cell1.setCellValue("产品名称");
cell2.setCellValue("批准文号");
cell3.setCellValue("生产企业");
cell4.setCellValue("产品数量");
cell5.setCellValue("买方ID");
cell6.setCellValue("卖方ID");
// Setting the property "Name" for the favoured printer (name of
// IP address)
com.sun.star.beans.PropertyValue propertyValue[] =
new com.sun.star.beans.PropertyValue[1];
propertyValue[0] = new com.sun.star.beans.PropertyValue();
propertyValue[0].Name = "Name";
propertyValue[0].Value = args[ 0 ];
// Setting the name of the printer
xPrintable.setPrinter( propertyValue );
// Setting the property "Pages" so that only the desired pages
// will be printed.
propertyValue[0] = new com.sun.star.beans.PropertyValue();
propertyValue[0].Name = "Pages";
propertyValue[0].Value = args[ 2 ];
// Printing the loaded document
xPrintable.print( propertyValue );
这是 demo:
https://wiki.openoffice.org/wiki/File:DocumentHandling.zip