word 进行数据库 获取数据
2014-12-18 15:16
155 查看
/**
* 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;
}
* 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;
}
相关文章推荐
- Asp获取数据库中的数据插入Word 时 怎么控制Word内容换行
- radiobuttonlist动态绑定、获取数据中数据进行选中以及写入数据库
- android利用servlet获取服务器数据库数据进行登录
- 获取博客积分排名,存入数据库,读取数据进行绘图(python,selenium,matplotlib)
- Python数据获取-文件、word、Excel、数据库
- java从前端获取excel表并进行解析,将数据保存数据库
- Silverlight调用自定义的Web Service从数据库获取数据进行页面绑定
- Echarts报表插件,从数据库获取数据,json传值,在页面进行展示
- DetailsView中如何进行数据验证,如何处理数据库的异常
- 关于用VC,VB进行图像数据(二进制大对象)存储数据库的一点心得
- [黑客技术][分享]直接获取access、SQL Server等数据库数据
- 利用Sql Server2000的规则对数据库中的数据进行限制
- 利用工具进行数据库数据的复杂查询 [转]
- 我鼓捣的数据库数据生成word文档的方法!
- ADO.Net都有哪些从数据库获取数据的方式
- 从数据库导出数据到word、excel、.txt
- 关于NTKO_office的操作(从数据库中提取数据,写入到NTKO_office_Word中)
- 系统损坏,移植Oracle(9.2.0.1)数据库(无备份数据文件进行恢复)
- ADO.Net都有哪些从数据库获取数据的方式
- 利用.net替换Word的内容(从数据库中取数据来替换word里面的书签)