* Word導出
* @throws DocumentException
*/
public void exeportWord() throws DocumentException{
//放到tomcat下
String formName="word";
String pathName=ServletActionContext.getRequest().getSession().getServletContext().getRealPath("")+"\\export\\"+formName+".doc";
//4.1定義表頭
String tableName="T_USER";
String tableCName = selectDataService.getString("select comments from user_tab_comments where table_name ='"+tableName+"'");
//4.2獲取表的中文註釋
List<Map> list=selectDataService.queryForList("select t.column_name,t.comments from user_col_comments t where table_name ='"+tableName+"'");
//4.2 創建一個list
List<String> columns=new ArrayList<String>();
Map<String, String> colmap=new TreeMap<String, String>();
for (Map map:list) {
//把數據庫裏的註釋取出來,放到columns裏
columns.add(map.get("COMMENTS").toString());
//把數據庫的中文注釋放到map集合中
colmap.put(map.get("COMMENTS").toString(), map.get("COLUMN_NAME").toString());
}
FileOutputStream outputStream=null;
Document document=null;
try {
//創建輸出流
outputStream=new FileOutputStream(pathName);
//創建document對象
document=new Document(PageSize.A4);
//輸出流 與document 進行關聯
RtfWriter2.getInstance(document, outputStream);
//打開document
document.open();
//定義字體
BaseFont createFont = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font font=new Font(createFont,10,Font.BOLD);
//寫入字體
Paragraph paragraph=new Paragraph("用戶管理",font);
//把他放到中間
paragraph.setAlignment("center");
//保存到document
document.add(paragraph);
//換行
document.add(new Paragraph("\n",font));
// 從數據庫裏取出所有數據
List<Map> dataList=selectDataService.queryForList("select * from "+tableName);
//寫表格
PdfPTable pdf= new PdfPTable(columns.size());
PdfPCell cell=null;
//創建表格
for(int i=0;i<11;i++){
String zhang = columns.get(i);
cell=new PdfPCell(new Phrase(zhang, font));
pdf.addCell(cell);
}
for (int i = 0; i < dataList.size(); i++) {
//獲取一行一行的數據
Map cols=dataList.get(i);
//j循環是循環一條數據裏的每個值
for (int j = 0; j < cols.size(); j++) {
//放到每個單元格里
String fangyuange = columns.get(j);
//去英文字段
String ing = colmap.get(fangyuange);
//放到列種
Object value = cols.get(ing);
//創建單元格(通過行)放進單元格
cell = new PdfPCell(new Phrase(""+value,font));
pdf.addCell(cell);
}
}
document.add(pdf);
// ServletActionContext.getResponse().getWriter().write("ok");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
document.close();
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
FileUtil.downloadFile(ServletActionContext.getRequest(), ServletActionContext.getResponse(), pathName, formName+".doc"); //看圖片
// return null;
}