java导出图⽚到Excel,并通过浏览器下载Excel
创建 Excel 表格
public void outExcel(Integer fileId, HttpServletResponse response){
//根据⽂件id查询档案对象
XzFile file = FileById(fileId);
if (file==null || file.equals("")){
return;
}
//创建⼯作簿对象
HSSFWorkbook wb = new HSSFWorkbook();
//建⽴新的sheet对象(excel的表单)
HSSFSheet sheet = wb.Name());
//在sheet⾥创建第⼀⾏,参数为⾏索引(excel的⾏),可以是0~65535之间的任何⼀个
HSSFRow row = ateRow(0);
//创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何⼀个
HSSFCell cell = ateCell(0);
//设置单元格的值
cell.setCellValue("登记卡");
//合并单元格CellRangeAddress构造参数依次表⽰起始⾏,截⾄⾏,起始列,截⾄列
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
//多值判断
CellIsWhatUtil cellIsWhatUtil = new CellIsWhatUtil();
/
/在sheet中创建第⼆⾏
HSSFRow row2 = ateRow(1);
//添加单元格中的内容
HSSFCell cell6 = ateCell(6);
if (IdPhoto() == null || IdPhoto().equals("")) {
connect下载cell6.setCellValue("贴相⽚处\n" + "(2⼨⽩底)");
} else {
// 利⽤HSSFPatriarch将图⽚写⼊EXCEL
HSSFPatriarch patriarch = ateDrawingPatriarch();
//拿到图⽚的⼆进制数据 cellIsWhatUtil:图⽚转为⼆进制⼯具类
byte[] imgData = IdPhoto());
//anchor主要⽤于设置图⽚的属性
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 7, 1, (short) 6, 4);
//Sets the anchor type (图⽚在单元格的位置)
//0 = Move and size with Cells, 2 = Move but don't size with cells, 3 = Don't move or size with cells.
anchor.setAnchorType(3);
}
sheet.addMergedRegion(new CellRangeAddress(1, 4, 6, 6));
//在sheet中创建第3⾏
HSSFRow row3 = ateRow(2);
//添加单元格中的内容
//在sheet中创建第4⾏
HSSFRow row4 = ateRow(3);
//添加单元格中的内容
//合并单元格CellRangeAddress构造参数依次表⽰起始⾏,截⾄⾏,起始列,截⾄列 sheet.addMergedRegion(new CellRangeAddress(4, 4, 3, 5));
//在sheet中创建第5⾏
HSSFRow row5 = ateRow(4);
//添加单元格中的内容
//在sheet中创建第6⾏
HSSFRow row6 = ateRow(5);
//添加单元格中的内容
/
/合并单元格CellRangeAddress构造参数依次表⽰起始⾏,截⾄⾏,起始列,截⾄列 sheet.addMergedRegion(new CellRangeAddress(5, 5, 1, 6));
//在sheet中创建第7⾏
HSSFRow row7 = ateRow(6);
//添加单元格中的内容
//合并单元格CellRangeAddress构造参数依次表⽰起始⾏,截⾄⾏,起始列,截⾄列 sheet.addMergedRegion(new CellRangeAddress(6, 6, 1, 6));
//在sheet中创建第8⾏
HSSFRow row8 = ateRow(7);
//添加单元格中的内容
//合并单元格CellRangeAddress构造参数依次表⽰起始⾏,截⾄⾏,起始列,截⾄列 sheet.addMergedRegion(new CellRangeAddress(7, 7, 1, 6));
//设置每⾏的⾼度
row.setHeightInPoints(30);
row2.setHeightInPoints(20);
row3.setHeightInPoints(20);
row4.setHeightInPoints(20);
row5.setHeightInPoints(30);
row6.setHeightInPoints(30);
row7.setHeightInPoints(30);
row8.setHeightInPoints(30);
//设置每列的宽度
int[] width = {3000, 3000, 3000, 3000, 3000, 3000, 3000};
for (int i = 0; i < width.length; i++) {
sheet.setColumnWidth(i, width[i]);
}
// row1
HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFFont fontStyle = wb.createFont();
/*//设置上下左右四个边框宽度
cellStyle.setBorderTop(HSSFBorderFormatting.BORDER_THIN);
cellStyle.setBorderBottom(HSSFBorderFormatting.BORDER_THIN);
cellStyle.setBorderLeft(HSSFBorderFormatting.BORDER_THIN);
cellStyle.setBorderRight(HSSFBorderFormatting.BORDER_THIN);
//设置上下左右四个边框颜⾊
cellStyle.setTopBorderColor(HSSFColor.RED.index);
cellStyle.setBottomBorderColor(HSSFColor.RED.index);
cellStyle.setLeftBorderColor(HSSFColor.RED.index);
cellStyle.setRightBorderColor(HSSFColor.RED.index);*/
//⽔平居中
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//垂直居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置⾃动换⾏
cellStyle.setWrapText(true);
//设置字体样式
fontStyle.setFontName("宋体");
//设置字体⾼度
fontStyle.setFontHeightInPoints((short) 15);
//设置字体颜⾊
fontStyle.setColor(HSSFColor.BLACK.index);
//设置粗体
fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
/
/设置斜体
// fontStyle.setItalic(true);
// fontStyle.setUnderline(HSSFFont.U_SINGLE);
//字体也是单元格格式的⼀部分,所以从属于HSSFCellStyle
// 将字体对象赋值给单元格样式对象
cellStyle.setFont(fontStyle);
// 将单元格样式应⽤于单元格
cell.setCellStyle(cellStyle);
// row2,3,4
HSSFCellStyle cellStyle2 = wb.createCellStyle();
HSSFFont fontStyle2 = wb.createFont();
/
/⽔平居中
cellStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//垂直居中
cellStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//设置⾃动换⾏
cellStyle2.setWrapText(true);
//设置字体样式
fontStyle2.setFontName("宋体");
//设置字体⾼度
fontStyle2.setFontHeightInPoints((short) 7);
//设置字体颜⾊
fontStyle2.setColor(HSSFColor.BLACK.index);
//设置粗体
fontStyle2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 将字体对象赋值给单元格样式对象
cellStyle2.setFont(fontStyle2);
//第⼆⾏
for (int i = 0; i < 7; i++) {
}
//第三四⾏
for (int i = 0; i < 6; i++) {
}
//第五⾏
for (int i = 0; i < 4; i++) {
}
//第6,7,8⾏
for (int i = 0; i < 2; i++) {
}
//输出Excel⽂件
// FileOutputStream fileOutputStream = null;
OutputStream output;
try {
output = OutputStream();
//清空缓存
//定义浏览器响应表头,顺带定义下载名,⽐如students(中⽂名需要转义)
String s = Name() + Birthday();
response.setHeader("Content-disposition", "attachment;filename=" + new Bytes(), "iso-8859-1") + ".xls");
//定义下载的类型,标明是excel⽂件
response.setContentType("application/vnd.ms-excel");
//这时候把创建好的excel写⼊到输出流
wb.write(output);
//养成好习惯,出门记得随⼿关门
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
cellIsWhatUtil⼯具类:拿到图⽚的⼆进制数据
public byte[] getImgData(String pictureUrl){
if(StringUtils.isNotBlank(pictureUrl)) {
if (pictureUrl==null || pictureUrl.equals("")){
return null;
}
String str = "*****/temp/";
//路径有汉字需要转码 String url = “l****/test?condition=”+de("⼯⼚","utf-8");
int i = pictureUrl.lastIndexOf("/");
String substring = pictureUrl.substring(i+1);
URL url = null;
try {
url = new URL(str+ de(substring,"utf-8"));
//打开链接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置请求⽅式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
//通过输⼊流获取图⽚数据
InputStream inStream = InputStream();
//得到图⽚的⼆进制数据,以⼆进制封装得到数据,具有通⽤性
byte[] data = readInputStream(inStream);// 得到图⽚的⼆进制数据
return data;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论