java二级 PDF_Java操作Excel、PDF文件(2)

3、用于Excel视图的视图子类化

为了在生成输出文档的过程中实现定制的行为,我们将继承合适的抽象类。对于Excel,这包括提供一个 org.springframework.web.servlet.view.document.AbstractExcelView的子类,并实现 buildExcelDocument方法。

Java代码

public class ViewExcel extends AbstractExcelView {

public void buildExcelDocument(

Map model, HSSFWorkbook workbook,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

HSSFSheet sheet = workbook.createSheet("list");

sheet.setDefaultColumnWidth((short) 12);

HSSFCell cell = getCell(sheet, 0, 0);

setText(cell, "Spring Excel test");

HSSFCellStyle dateStyle = workbook.createCellStyle();

dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));

cell = getCell(sheet, 1, 0);

cell.setCellValue(new Date());

cell.setCellStyle(dateStyle);

getCell(sheet, 2, 0).setCellValue(458);

HSSFRow sheetRow = sheet.createRow(3);

for (short i = 0; i < 10; i++) {

sheetRow.createCell(i).setCellValue(i * 10);

}

}

}

public class ViewExcel extends AbstractExcelView {

public void buildExcelDocument(

Map model, HSSFWorkbook workbook,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

HSSFSheet sheet = workbook.createSheet("list");

sheet.setDefaultColumnWidth((short) 12);

HSSFCell cell = getCell(sheet, 0, 0);

setText(cell, "Spring Excel test");

HSSFCellStyle dateStyle = workbook.createCellStyle();

dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));

cell = getCell(sheet, 1, 0);

cell.setCellValue(new Date());

cell.setCellStyle(dateStyle);

getCell(sheet, 2, 0).setCellValue(458);

HSSFRow sheetRow = sheet.createRow(3);

for (short i = 0; i < 10; i++) {

sheetRow.createCell(i).setCellValue(i * 10);

}

}

}

4、用于PDF视图的视图子类化

需要象下面一样继承org.springframework.web.servlet.view.document.AbstractPdfView,并实现buildPdfDocument()方法。

Java代码

public class ViewPDF extends AbstractPdfView {

public void buildPdfDocument(Map model, Document document,

PdfWriter writer, HttpServletRequest request,

HttpServletResponse response) throws Exception {

List list = (List) model.get("list");

for (int i = 0; i < list.size(); i++)

document.add(new Paragraph((String) list.get(i)));

}

}

public class ViewPDF extends AbstractPdfView {

public void buildPdfDocument(Map model, Document document,

PdfWriter writer, HttpServletRequest request,

HttpServletResponse response) throws Exception {

List list = (List) model.get("list");

for (int i = 0; i < list.size(); i++)

document.add(new Paragraph((String) list.get(i)));

}

}

5、其他文件

1)控制器ViewController

Java代码

public class ViewController extends MultiActionController{

public ModelAndView viewPDF(HttpServletRequest request, HttpServletResponse response) throws Exception {

List list = new ArrayList();

Map model=new HashMap();

list.add("test1");

list.add("test2");

model.put("list",list);

ViewPDF viewPDF=new ViewPDF();

return new ModelAndView(viewPDF,model);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值