前端
Export() {
let pointEMT =["EMT.APProductionKWH3","EMT.APProductionKWH1"]; let pointPCS =["PCS.TotalChargePowerkWh","PCS.TotalDischargePowerkWh"]; let pointEMS =["BMS.Voltage","BMS.Current""]; let data={
"site_id":"1bcaecdcc", "interval": "60", "begin_time": "2018-07-10 15:15:10", "end_time": "2018-07-10 17:15:10", "objects":[{
"object_id": ["1bcac6635a","1bcae31be"], "domain_id": "348","point":pointPCS}] }; let baseUrl = process.env.API_ROOT; let apilUrl = "//172.20.34.19:8082" + "/api/downExcel"; axios({
// 用axios发送post请求 method: "post", url: apilUrl, // 请求地址 data: data, // 参数 responseType: "arraybuffer" // 表明返回服务器返回的数据类型 }).then(res => {
debugger // 处理返回的文件流 console.log(res); let time = new Date(); let year = time.getFullYear(); let month = time.getMonth() + 1; let day = time.getDate(); let fileName =this.form.name+".xlsx"; let fileDownload = require("js-file-download"); //fileDownload(response.data, vm.ascii2native(response.headers['x-suggested-filename'])) fileDownload(res.data, fileName); }); },
后端
public void outSheet(HttpServletRequest request, HttpServletResponse response, List<Encapsula> listTitle, List<SingleRow> singleRowList)throws Exception {
//1.创建工作簿 SXSSFWorkbook workbook = new SXSSFWorkbook(100); //2.创建工作表7 SXSSFSheet sheet = (SXSSFSheet) workbook.createSheet("数据导出"); //设置默认列宽 sheet.setDefaultColumnWidth(20); //1.1标题样式 XSSFCellStyle colStyle = createCellStyle(workbook,(short)10,true,true); //内容样式 XSSFCellStyle cellStyle = createCellStyle(workbook,(short)10,false,true); //2.创建合并单格对象 //标题、时间 CellRangeAddress callRangeAddress = new CellRangeAddress(0,1,0,0);//起始行,结束行,起始列,结束列 sheet.addMergedRegion(callRangeAddress); SXSSFRow row = (SXSSFRow) sheet.createRow(0); SXSSFCell cell = (SXSSFCell) row.createCell(0); //加载单格样式 cell.setCellStyle(colStyle); cell.setCellValue("时间"); row = (SXSSFRow) sheet.createRow(1); cell = (SXSSFCell) row.createCell(0); cell.setCellValue(" "); //主表头 SXSSFRow hssfRow2 = (SXSSFRow) sheet.getRow(0); if (hssfRow2 == null) {
hssfRow2 = (SXSSFRow) sheet.createRow(0); } int viceTitle = 0; for(int i = 0; i < listTitle.size(); i++){
for(int j = 0; j < listTitle.get(i).getDevice_names().size(); j++){
viceTitle++; SXSSFCell cell2 = (SXSSFCell) hssfRow2.createCell(viceTitle); cell2.setCellValue(listTitle.get(i).getDevice_names().get(j)); cell2.setCellStyle(colStyle); } } //副表头 SXSSFRow hssfRow1 = (SXSSFRow) sheet.getRow(1); if (hssfRow1 == null) {
hssfRow1 = (SXSSFRow) sheet.createRow(1); } int primaryTitle = 0; for(int i = 0; i < listTitle.size(); i++){
String name = listTitle.get(i).getDesc(); for(int j = 0; j < listTitle.get(i).getDevice_names().size(); j++){
primaryTitle++; SXSSFCell cell1 = (SXSSFCell) hssfRow1.createCell(primaryTitle); cell1.setCellValue(name); cell1.setCellStyle(colStyle); } } //填写数据单格 SXSSFRow hssfRow = (SXSSFRow) sheet.getRow(0); if (hssfRow == null) {
hssfRow = (SXSSFRow) sheet.createRow(0); } for(int i = 0; i < singleRowList.size(); i++){
SingleRow singleRow = new SingleRow(); singleRow = singleRowList.get(i); hssfRow = (SXSSFRow) sheet.createRow(i+2); SXSSFCell singleCell = (SXSSFCell) hssfRow.createCell(0); singleCell.setCellValue(singleRow.getTimestamp()); for(int j = 0; j < singleRow.getValues().size(); j++){
singleCell = (SXSSFCell) hssfRow.createCell(j+1); if(singleRow.getValues().get(j).equals("--") || singleRow.getValues().get(j).equals(" ")){
singleCell.setCellValue( singleRow.getValues().get(j)); }else {
singleCell.setCellValue(Double.valueOf( singleRow.getValues().get(j))); } singleCell.setCellStyle(cellStyle); } } //5.输出 writeExcel(response, workbook); //关闭流 System.out.println("导出成功"); } public XSSFCellStyle createCellStyle(SXSSFWorkbook workbook, short fontsize,boolean flag, boolean flag1) {
// TODO Auto-generated method stub XSSFCellStyle style = (XSSFCellStyle) workbook.createCellStyle(); //是否水平居中 if(flag1){
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 } style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 // 生成一个字体 XSSFFont font = (XSSFFont) workbook.createFont(); // 指定当单格内容显示不下时自动换行 style.setWrapText(true); //是否加粗字体 if(flag){
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); } font.setFontHeightInPoints(fontsize); //把字体应用到当前的样式 style.setFont(font); return style; } private void writeExcel(HttpServletResponse response, Workbook work) throws IOException {
OutputStream out = null; try {
out = response.getOutputStream(); //response.reset();// 清空输出流 response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Access-Control-Allow-Origin", "*"); work.write(out); } catch (IOException e) {
System.out.println("输出流错误"); e.printStackTrace(); } finally {
out.flush(); out.close(); } }
今天的文章
Java处理Excel操作分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/92173.html