POI如何根据:数据库中存的图片路径,需要拼接服务器地址才能显示出来图片,如何图片导入word文档中?

weixin_38290853 2017-08-03 01:54:52
第一种方法:这样写报错了,报错的原因是:添加图片的content不能那样写,那应该怎么做呢?代码如下:
@RequestMapping("/downloadImage")
public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
//从数据库查询图片路径的集合
List<WeiBo> urlList = weiBoService.selectWeiBoImageUrl();
String imageUrl = null;
String content = "";
for (int i= 0 ;i<urlList.size();i++){
WeiBo weiBo = (WeiBo) urlList.get(i);
imageUrl = weiBo.getImageUrl();
content += "http://110.101.108.115:9091/"+imageUrl+ "\n";
}
//创建Document对象(word文档) author:zxh Sep 15, 2010
Rectangle rectPageSize = new Rectangle(PageSize.A4);
rectPageSize = rectPageSize.rotate();
// 创建word文档,并设置纸张的大小
Document doc = new Document(PageSize.A4);
String fileName="E:/企业详细信息登记表_"+System.currentTimeMillis()+".doc";
//建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中 author:yyli Sep 15, 2010
RtfWriter2.getInstance(doc, new FileOutputStream(fileName));
doc.open();
//标题字体 author:yyli Sep 15, 2010
RtfFont titleFont = new RtfFont("仿宋_GB2312", 15, Font.BOLD,
Color.BLACK);
// 正文字体 author:yyli Sep 15, 2010
RtfFont contextFont = new RtfFont("仿宋_GB2312", 9, Font.NORMAL,
Color.BLACK);
// 表格设置 author:yyli Sep 15, 2010
Table table = new Table(4, 1);
int[] withs = { 15, 35, 15, 1};
// 设置每列所占比例 author:yyli Sep 15, 2010
table.setWidths(withs);
// 表格所占页面宽度 author:yyli Sep 15, 2010
table.setWidth(100);
// 居中显示 author:yyli Sep 15, 2010
table.setAlignment(Element.ALIGN_CENTER);
// 自动填满 author:yyli Sep 15, 2010
table.setAutoFillEmptyCells(true);
table.setBorderWidth(5); // 边框宽度
table.setBorderColor(new Color(0, 125, 255)); // 边框颜色
table.setPadding(12);// 衬距,看效果就知道什么意思了
table.setSpacing(0);// 即单元格之间的间距
table.setBorder(5);// 边框
// 第一行(标题) author:yyli Sep 15, 2010
String titleString = "";
Paragraph title = new Paragraph(titleString);
// 设置标题格式对其方式
title.setAlignment(Element.ALIGN_CENTER);
title.setFont(titleFont);
doc.add(title);
//第二行(正文) author:yyli Sep 15, 2010
@SuppressWarnings("deprecation")
String contextString = "";
Paragraph context = new Paragraph(contextString);
// 正文格式对齐方式
context.setAlignment(Element.ALIGN_RIGHT);
context.setFont(contextFont);
// 与上一段落(标题)的行距
context.setSpacingBefore(10);
// 设置第一行空的列数(缩进)
// context.setFirstLineIndent(20);
doc.add(context);
Cell cell=null;

//在列中添加图片
Image png = Image.getInstance(content);
cell=new Cell(png);
cell.setColspan(3);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
doc.add(table);
doc.close();
}

第二种方法:这种方法,导入word文档里的是;服务器地址+数据库图片的路径!!!!没有图片显示!代码如下:
@RequestMapping("/downloadImage")
public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {

List<WeiBo> urlList = weiBoService.selectWeiBoImageUrl();
String imageUrl = null;
String content = "";
for (int i= 0 ;i<urlList.size();i++){
WeiBo weiBo = (WeiBo) urlList.get(i);
imageUrl = weiBo.getImageUrl();
content += "http://110.101.108.115:9091/"+imageUrl+ "\n";
}
try {
byte b[] = content.getBytes("utf-8"); //这里是必须要设置编码的,不然导出中文就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
/**
* 关键地方
* 生成word格式
*/
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
//输出文件
String fileName = "tupian";
request.setCharacterEncoding("utf-8");
response.setContentType("application/msword");//导出word格式
response.addHeader("Content-Disposition", "attachment;filename=" +
new String((fileName + ".doc").getBytes(),
"UTF-8"));
OutputStream ostream = response.getOutputStream();
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
} catch (Exception e) {
logger.error("导出出错:%s", e.getMessage());
}
}


...全文
334 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
JE_GE 2017-08-04
  • 打赏
  • 举报
回复
http://blog.csdn.net/je_ge/article/details/53410364

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧