前台
<form id="exportForm" action="/foodBreak/statistical/importData" method="post"> <input type="hidden" name="img" id="img" /> <input type="hidden" name="dataText" id="dataText" /> </form>
$('#importBtn').live('click', function () { var data = myChart.getDataURL("png"); $("#img").val(data); $('#dataText').val('a_b_c_d,0_2_1_2'); $("#exportForm").submit(); });
<script type="text/javascript"> var myChart = ''; function loadMap(areaList,countryList, provinceList,cityList,reportDeleteList) { var dom = document.getElementById("container"); myChart = echarts.init(dom); var app = {}; option = null; option = { title : { text: '11', subtext: '' }, tooltip : { trigger: 'axis' }, legend: { data:['1','2','3','4'] }, toolbox: { show : true, feature : { dataView : {show: true, readOnly: false}, magicType : {show: true, type: ['line', 'bar']}, restore : {show: true}, saveAsImage : {show: true} } }, calculable : true, xAxis : [ { type : 'category', data : areaList/*['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']*/ } ], yAxis : [ { type : 'value' } ], series : [ { name:'1', type:'bar', data:countryList, markPoint : { data : [ {type : 'max', name: '最大值'}, {type : 'min', name: '最小值'} ] } /*markLine : { data : [ {type : 'average', name: '平均值'} ] }*/ }, { name:'2', type:'bar', data:provinceList, markPoint : { data : [ {type : 'max', name: '最大值'}, {type : 'min', name: '最小值'} ] } /*markLine : { data : [ {type : 'average', name : '平均值'} ] }*/ }, { name:'3', type:'bar', data:cityList, markPoint : { data : [ {type : 'max', name: '最大值'}, {type : 'min', name: '最小值'} ] }/*, markLine : { data : [ {type : 'average', name : '平均值'} ] }*/ }, { name:'4', type:'bar', data:reportDeleteList, markPoint : { data : [ {type : 'max', name: '最大值'}, {type : 'min', name: '最小值'} ] }/*, markLine : { data : [ {type : 'average', name : '平均值'} ] }*/ } ] }; if (option && typeof option === "object") { myChart.setOption(option, true); } console.log(myChart.getDataURL("png")); } </script>
后台
/**
* importData:(导出数据). <br/>
*
* /foodBreak/statistical/importData
*
* @author wpengfei
* @param inv
* @return
* @since JDK 1.6
*/
@Post("/importData")
public String importData(Invocation inv) {
String data = inv.getRequest().getParameter("img");
String dataText = inv.getRequest().getParameter("dataText");
if (StringUtils.isNotBlank(dataText)) {
try {
// String userName = System.getProperty("user.name");
// logger.info("userName:" + userName);
String filePath = Constants.UPLOAD_BASE_FOLD;
File file = new File(filePath);
if (!file.exists()) {
file.mkdir();
}
String fileName = filePath + "\\" + System.currentTimeMillis() + ".png";
fbStatisticalInfoService.createImage(inv.getRequest(), inv.getResponse(), fileName, data);
fbStatisticalInfoService.createExcel(inv.getRequest(), inv.getResponse(), fileName, dataText);
} catch (Exception e) {
e.printStackTrace();
}
}
return "@";
}
import java.util.List;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;
/**
* createImage:(生成图片). <br/>
*
* @author wpengfei
* @param request
* @param response
* @param fileName
* @param data
* @throws ServletException
* @throws IOException
* @since JDK 1.6
*/
public void createImage(HttpServletRequest request, HttpServletResponse response,
String fileName, String data)
throws ServletException, IOException {
try {
String[] url = data.split(",");
String u = url[1];
// Base64解码
byte[] b = new BASE64Decoder().decodeBuffer(u);
// 生成图片
OutputStream out = new FileOutputStream(new File(fileName));
out.write(b);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* createExcel:(创建excel并导出). <br/>
*
* @author wpengfei
* @param request
* @param response
* @param fileName
* @param dataText
* @throws ServletException
* @throws IOException
* @since JDK 1.6
*/
public void createExcel(HttpServletRequest request, HttpServletResponse response,
String fileName,String dataText)
throws ServletException, IOException {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("echart");
// 3.创建单元格样式
CellStyle cellStyle = wb.createCellStyle();
// 设置这些样式
cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
sheet.setDefaultColumnWidth(20);
List<String> list = ITCastStringUtils.toStringList(dataText);
// 4.创建Excel工作表的行
int i=0;
HSSFRow row = null;
String[] strList = null;
for (String view : list) {
strList = view.split("[_]");
row = sheet.createRow(i);
row.createCell(0).setCellValue(strList[0]);
row.createCell(1).setCellValue(strList[1]);
row.createCell(2).setCellValue(strList[2]);
row.createCell(3).setCellValue(strList[3]);
row.createCell(4).setCellValue(strList[4]);
i++;
}
/*// 设置sheet名称和单元格内容
wb.setSheetName(0, "aa"); */
/*导入图片*/
/*HSSFCell headerCell = row.createCell(0);
headerCell.setCellType(HSSFCell.CELL_TYPE_BLANK);
headerCell.setCellValue("echarts");*/
// HSSFCell cells = row.createCell(0);
// cells.setCellType(HSSFCell.CELL_TYPE_BLANK);
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); // 将图片写入流中
BufferedImage bufferImg = ImageIO.read(new File(fileName));
ImageIO.write(bufferImg, "PNG", outStream); // 利用HSSFPatriarch将图片写入EXCEL
int length = list.size();
HSSFPatriarch patri = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(length + 200, length + 200, length + 200, length + 200,
(short) 1, length + 5, (short) 7, length + 40);
patri.createPicture(anchor, wb.addPicture(
outStream.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
try {
OutputStream out = null;
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("echarts.xls", "UTF-8"));
out = response.getOutputStream();
wb.write(out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
关于HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2)的参数,有必要在这里说明一下:
dx1:起始单元格的x偏移量,如例子中的255表示直线起始位置距A1单元格左侧的距离;
dy1:起始单元格的y偏移量,如例子中的125表示直线起始位置距A1单元格上侧的距离;
dx2:终止单元格的x偏移量,如例子中的1023表示直线起始位置距C3单元格左侧的距离;
dy2:终止单元格的y偏移量,如例子中的150表示直线起始位置距C3单元格上侧的距离;
col1:起始单元格列序号,从0开始计算;
row1:起始单元格行序号,从0开始计算,如例子中col1=0,row1=0就表示起始单元格为A1;
col2:终止单元格列序号,从0开始计算;
row2:终止单元格行序号,从0开始计算,如例子中col2=2,row2=2就表示起始单元格为C3;