package org.rosenjiang.servlet; import java.awt.Color; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import org.rosenjiang.service.UserService; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Font; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfPCell; import com.lowagie.text.pdf.PdfPTable; import com.lowagie.text.pdf.PdfWriter; public class ReportServlet extends HttpServlet { /** * Return a PDF document for download. * */ public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String account_id = request.getParameter("account_id"); String search_date_from = request.getParameter("search_date_from"); String to = request.getParameter("to"); WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext()); UserService userService = (UserService)ctx.getBean("userService"); List<Map<String, Object>> list = userService.getAccountActivity(account_id, search_date_from, to); // create PDF document Document document = new Document(); try { //set response info response.setContentType("application/x-msdownload;charset=UTF-8"); response.setHeader("Content-Disposition","attachment;filename=report.pdf"); //open output stream PdfWriter.getInstance(document, response.getOutputStream()); // open PDF document document.open(); // set chinese font BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font f2 = new Font(bfChinese, 2, Font.NORMAL); Font f6 = new Font(bfChinese, 6, Font.NORMAL); Font f8 = new Font(bfChinese, 8, Font.NORMAL); Font f10 = new Font(bfChinese, 10, Font.NORMAL); Font f12 = new Font(bfChinese, 12, Font.BOLD); //set title document.add(new Paragraph("金融报表", f12)); //<br> document.add(new Paragraph(" ",f6)); //set sub title document.add(new Paragraph("账户信息", f10)); //<br> document.add(new Paragraph(" ", f2)); //process business data if(list.size()>0 && list.get(0).get("bankbook_no")!=null){ float openBalance = 0; //create table with 7 columns PdfPTable table = new PdfPTable(7); //100% width table.setWidthPercentage(100); table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT); //create cells PdfPCell cell = new PdfPCell(); //set color cell.setBackgroundColor(new Color(213, 141, 69)); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // cell.setPhrase(new Paragraph("交易日", f8)); table.addCell(cell); cell.setPhrase(new Paragraph("类型", f8)); table.addCell(cell); cell.setPhrase(new Paragraph("备注", f8)); table.addCell(cell); cell.setPhrase(new Paragraph("ID", f8)); table.addCell(cell); cell.setPhrase(new Paragraph("票号", f8)); table.addCell(cell); cell.setPhrase(new Paragraph("合计", f8)); table.addCell(cell); cell.setPhrase(new Paragraph("余额", f8)); table.addCell(cell); //create another cell PdfPCell newcell = new PdfPCell(); newcell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); Map<String, Object> map = new HashMap<String, Object>(); for(int i = 0; i < list.size(); i++){ map = list.get(i); String cashInout = map.get("cash_inout").toString(); newcell.setPhrase(new Paragraph(map.get("trade_date").toString(), f8)); table.addCell(newcell); newcell.setPhrase(new Paragraph(map.get("bankbook_type").toString(), f8)); table.addCell(newcell); newcell.setPhrase(new Paragraph(map.get("memo").toString(), f8)); table.addCell(newcell); newcell.setPhrase(new Paragraph(map.get("account_id").toString(), f8)); table.addCell(newcell); newcell.setPhrase(new Paragraph(map.get("ticket_no").toString(), f8)); table.addCell(newcell); newcell.setPhrase(new Paragraph(map.get("amount").toString(), f8)); table.addCell(newcell); newcell.setPhrase(new Paragraph(openBalance+"", f8)); table.addCell(newcell); if(cashInout.equals("I")){ openBalance = openBalance + Float.valueOf(map.get("amount").toString()); }else if(cashInout.equals("O")){ openBalance = openBalance - Float.valueOf(map.get("amount").toString()); } } //print total column newcell.setPhrase(new Paragraph("合计"+openBalance, f8)); table.addCell(""); table.addCell(""); table.addCell(""); table.addCell(""); table.addCell(""); table.addCell(""); table.addCell(newcell); document.add(table); }else{ PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100); table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT); PdfPCell cell = new PdfPCell(new Paragraph("暂无数据")); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); table.addCell(cell); document.add(table); } } catch(DocumentException de) { de.printStackTrace(); System.err.println("document: " + de.getMessage()); }finally{ // close the document and the outputstream is also closed internally document.close(); } } }
------------------------------?
table = new PdfPTable(2);
? ? ? ? ? ? ? ? table.setTotalWidth(pageTableWidth);
? ? ? ? ? ? ? ? table.setWidthPercentage(100);
? ? ? ? ? ? ? ? float[] widths = {5f, 95f};
? ? ? ? ? ? ? ? table.setWidths(widths);
? ? ? ? ? ? ? ? dCell = table.getDefaultCell();
? ? ? ? ? ? ? ? dCell.setBorder(Rectangle.NO_BORDER);
? ? ? ? ? ? ? ? dCell.setBorderWidth(0);
? ? ? ? ? ? ? ? dCell.setPadding(1);
?
? ? ? ? ? ? ? ? PdfPCell desc = new PdfPCell(new Phrase(""));
? ? ? ? ? ? ? ? desc.setBorder(0);
? ? ? ? ? ? ? ? desc.setColspan(2);
--------------------------------------------------
?