java导出表格PDF
第一次寫博客,有很多不足的地方請多多指教,話不多說,直接上代碼。
1.建立PDF模板
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.awt.Color;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
?* <p>Title: 生成PDF文件</p>
?* <p>Description: 本實例通過使用iText包生成一個表格的PDF文件</p>
?* <p>Copyright: Copyright (c) 2018</p>
?* <p>Filename: DbspreadDataPDF.java</p>
?* @author ly
?* @version 1.0
?*/
public class DbspreadDataPDF {
? ? /**
? ? ?*<br>方法說明:寫PDF文件
? ? ?*<br>輸入參數:
? ? ?*<br>返回類型:
? ? ?*@param pdfname 文件名稱
? ? ?*@param headName 表頭名稱
? ? ?*@param headcount 表頭信息
? ? ?*@param list 數據源
? ? ?*@param names 對應數據源的屬性
? ? ?*/
? ? ? public void write(HttpServletRequest request, HttpServletResponse response,String pdfname,String headName,String headcount[],List<Map<String, Object>> list,String[] names){
? ? ? ?try{
? ? ? ? ?Document document=new Document(PageSize.A4, 50, 50, 100, 50);
? ? ? ? ?Rectangle pageRect=document.getPageSize();
? ? ? ? ?response.setContentType("application/x-msdownload;charset=UTF-8");
? ? ? ? ?response.setHeader("Content-Disposition","attachment;filename="+pdfname+"");
? ? ? ? ?PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
? ? ? ? ?//創建漢字字體
? ? ? ? ?BaseFont bfSong = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
? ? ? ? ?Font fontSong = new Font(bfSong, 10, Font.NORMAL);
? ? ? ? ?
? ? ? ? ? // 為頁增加頁頭信息
? ? ? ? ?HeaderFooter header = new HeaderFooter(new Phrase(""+headName+"",fontSong), false);
? ? ? ? ?header.setBorder(2);
? ? ? ? ?header.setAlignment(Element.ALIGN_RIGHT);
? ? ? ? ?document.setHeader(header);
? ? ? ? ?
? ? ? ?// 為頁增加頁腳信息
? ? ? ? ?HeaderFooter footer = new HeaderFooter(new Phrase("第 ",fontSong),new Phrase(" 頁",fontSong));
? ? ? ? ?footer.setAlignment(Element.ALIGN_CENTER);
? ? ? ? ?footer.setBorder(1);
? ? ? ? ?document.setFooter(footer);
? ? ? ? ? // 打開文檔
? ? ? ? ?document.open();?
? ? ? ? ?//構造表格
? ? ? ? ?Table table = new Table(headcount.length);
? ? ? ? ?table.setAlignment(Element.ALIGN_MIDDLE);
? ? ? ? ?table.setBorder(Rectangle.NO_BORDER);
? ? ? ? ?int hws[] = new int[headcount.length] ;
? ? ? ? ?for (int i = 0; i < headcount.length; i++) {
? ? ? ? ? ? ?hws[i]=20;
? ? ? ? ?}
? ? ? ? ?table.setWidths(hws);
? ? ? ? ?table.setWidth(100);
? ? ? ? ?//表頭信息
? ? ? ? ?for (String i : headcount) {
? ? ? ? ? ? ?Cell cellleft= new Cell(new Phrase(i,new Font(bfSong, 10, Font.ITALIC,new Color(0,0,255))));
? ? ? ? ? ? ?cellleft.setUseAscender(true);?
? ? ? ? ? ? ?cellleft.setColspan(1);
? ? ? ? ? ? ?cellleft.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置字體垂直居中;
? ? ? ? ? ? ?cellleft.setHorizontalAlignment(Element.ALIGN_CENTER);//設置字體水平居中,只能通過cell來居中;
? ? ? ? ? ? ?table.addCell(cellleft);
? ? ? ? ?}
? ? ? ? ?//收貨和訂貨人信息,表體內容
? ? ? ? ?for (Map<String, Object> map : list) {
? ? ? ? ? ? ?for (int i = 0; i < names.length; i++) {
? ? ? ? ? ? ? ? ?if (map.get(names[i]) != null) {
? ? ? ? ? ? ? ? ? ? ?Cell cellleft= new Cell(new Phrase(map.get(names[i]).toString(),fontSong));
? ? ? ? ? ? ? ? ? ? ?cellleft.setUseAscender(true);?
? ? ? ? ? ? ? ? ? ? ?cellleft.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置字體垂直居中;
? ? ? ? ? ? ? ? ? ? ?cellleft.setHorizontalAlignment(Element.ALIGN_CENTER);//設置字體水平居中,只能通過cell來居中;
? ? ? ? ? ? ? ? ? ? ?table.addCell(cellleft);
? ? ? ? ? ? ? ? ?}else{
? ? ? ? ? ? ? ? ? ? ?Cell cellleft= new Cell(new Phrase("",fontSong));
? ? ? ? ? ? ? ? ? ? ?cellleft.setUseAscender(true);?
? ? ? ? ? ? ? ? ? ? ?cellleft.setVerticalAlignment(Element.ALIGN_MIDDLE);//設置字體垂直居中;
? ? ? ? ? ? ? ? ? ? ?cellleft.setHorizontalAlignment(Element.ALIGN_CENTER);//設置字體水平居中,只能通過cell來居中;
? ? ? ? ? ? ? ? ? ? ?table.addCell(cellleft);
? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ?}
? ? ? ? ?}
? ? ? ? ?//將表格添加到文本中
? ? ? ? ?document.add(table);
? ? ? ? ?//關閉文本,釋放資源
? ? ? ? ?document.close();?
? ? ? ? ?
? ? ? ?}catch(Exception e){
? ? ? ? ?System.out.println(e);? ?
? ? ? ?}
? ? ? }
}
2.我們寫一個測試類
?public static void main(String[] args) {
? ? ? ? List<T>? list=new ArrayList<T>(); //建立一個list集合,將你的數據源存儲到改歷史集合中
? ? ? ? List<Map<String, Object>> dbServicestypesMaplist? = new ArrayList<Map<String,Object>>();
? ? ? ? dbServicestypesMaplist = BeanToMapUtil.convertListBean2ListMap(list,A.class);//將list集合轉換成list map集合
? ? ? ? DbspreadDataPDF dbspreadDataPDF=new DbspreadDataPDF();
? ? ? ? String pdfname = Sys.getCtime()+".pdf";
? ? ? ? String headName="用戶統計";
? ? ? ? String[] headcount={"編號","用戶名","地址","手機號"};
? ? ? ? String[] names={"sid","username","address","phone"};
? ? ? ? dbspreadDataPDF.write(request, response, pdfname, headName, headcount, dbServicestypesMaplist, names);
? ? }
3.Map 對象與 JavaBean 對象互轉工具類
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
?* Map 對象與 JavaBean 對象互轉工具類
?*/
public class BeanToMapUtil<T> {
private BeanToMapUtil()
{
// 私有的構造方法
}
/**
* 將一個 JavaBean 對象轉化為一個 Map
*?
* @param bean
* @return
* @throws IntrospectionException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static Map<String, Object> convertBean2Map(Object bean) throws IntrospectionException, IllegalAccessException, InvocationTargetException {
Class<? extends Object> type = bean.getClass();
Map<String, Object> returnMap = new HashMap<>();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!"class".equals(propertyName)) {
if (propertyName.contains("time")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean, new Object[0]);
if (result != null) {
Timestamp ts = Timestamp.valueOf(result.toString());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
result = sdf.format(ts);
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, null);
}
} else {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean, new Object[0]);
if (result != null) {
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, null);
}
}
}
}
return returnMap;
}
/**
* 將 List<JavaBean>對象轉化為List<Map>
*?
* @param beanList
* @return
* @throws Exception
*/
public static <T> List<Map<String, Object>> convertListBean2ListMap(List<T> beanList, Class<T> T) throws Exception {
List<Map<String, Object>> mapList = new ArrayList<>();
for (int i = 0, n = beanList.size(); i < n; i++) {
Object bean = beanList.get(i);
Map<String, Object> map = convertBean2Map(bean);
mapList.add(map);
}
return mapList;
}
}
總結
以上是生活随笔為你收集整理的java导出表格PDF的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 哈哈 电控
- 下一篇: 新型消防机器人作文_未来的消防服作文50