twoYearsOld 2020-08-28 11:09 采纳率: 0%
浏览 159
已采纳

我把导出excel 用 try catch 来省略判空 代码可以导出excel 请问这样有影响 吗 一般实际上导出excel 的时间比较少我感觉应该没影响吧 如果数据多的话会有影响吗

package cn.itcast.web.controller.cargo;

import cn.itcast.dao.cargo.entrustDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.BoxService;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.service.cargo.entrustService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.DownloadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/shipping")
public class entrustController extends BaseController {
@Reference
private BoxService boxService;
@Reference
private ExportService exportService;
@Reference
private entrustService entrustService;
@RequestMapping("/toAdd")
public String toAdd(String id){

    //通过装箱id 查询 装箱对象
    Box box = boxService.findById(id);
    //把箱子的东西保存到域名里面
    request.setAttribute("box",box );
    //获取多条报运单
    String exportIds = box.getExportIds();
    //切割报运单 得到多个单包运单
    String[] split = exportIds.split(",");
    //因为要获取里面的  各大运输 而且 他们相同所有获取一条即可  查询报运单
    Export export = exportService.findById(split[0]);
    request.setAttribute("export",export);
    return "cargo/entrust/entrust-add";

}
@RequestMapping("/add")
public String add(Entrust entrust){


    entrust.setCompanyId(getLoginCompanyId());
    entrust.setCompanyName(getLoginCompanyName());
    //封装剩下的信息
    entrustService.add(entrust);
    return "redirect:/cargo/shipping/list.do";
}
@RequestMapping("/list")
public String list(@RequestParam(defaultValue = "1") Integer pageNum,
                   @RequestParam(defaultValue = "5") Integer pageSize){

    EntrustExample entrustExample = new EntrustExample();
    EntrustExample.Criteria criteria = entrustExample.createCriteria();
    criteria.andCompanyIdEqualTo(getLoginCompanyId());
    PageInfo pageInfo = entrustService.findByPage(entrustExample,pageNum,pageSize);
    request.setAttribute("pageInfo",pageInfo );
    return "cargo/entrust/entrust-list";
}
@RequestMapping("/delete")
public String delete(String id){
     entrustService.delete(id);
    return "redirect:/cargo/shipping/list.do";
}

@RequestMapping("/toInvoice")
public String toInvoice(String id){
return "";
}
@RequestMapping("/toExcel")
public void toExcel(String id) throws IOException {

    //读取Excel的模板
    InputStream inputStream = session.getServletContext()
            .getResourceAsStream("/make/xlsprint/tuoyun.xlsx");


    //1.创建工作簿
    XSSFWorkbook workbook = new XSSFWorkbook(inputStream);

    //2.获取第一张工作表
    XSSFSheet sheet = workbook.getSheetAt(0);

    //3.获取模板的内容行的每列的样式
    CellStyle[] cellStyles = new CellStyle[14];
    //获取标模板的样式
    for (int i = 0; i < cellStyles.length; i++) {
        cellStyles[i] = sheet.getRow(1).getCell(i).getCellStyle();
    }
    //查询数据
    EntrustExample entrustExample = new EntrustExample();
    EntrustExample.Criteria criteria = entrustExample.createCriteria();
    if (id != null && !id.equals("a")) {
        String[] split = id.substring(0, id.length()).split(",");

        criteria.andShippingOrderIdIn(Arrays.asList(split));
    }
    criteria.andCompanyIdEqualTo(getLoginCompanyId());
    List<Entrust> entrust = entrustService.findAll(entrustExample);

    //创建内容行
    XSSFCell cell = null;

/*
if (entrust!=null&&entrust.size()>0){
for (int i = 0; i < entrust.size(); i++) {
//取出每个数据
Entrust entrust1 = entrust.get(i);
XSSFRow row = sheet.createRow(2 + i);
//设置行高
row.setHeightInPoints(24);
//获取单元格
//设置里面的每个值
cell = row.createCell(0);
cell.setCellStyle(cellStyles[0]);
cell.setCellValue(entrust1.getShippingOrderId() != null ? entrust1.getShippingOrderId() : "");

            cell = row.createCell(1);
            cell.setCellStyle(cellStyles[1]);
            cell.setCellValue(entrust1.getOrderType() != null ? entrust1.getOrderType() : "");
            cell = row.createCell(2);
            cell.setCellStyle(cellStyles[2]);
            cell.setCellValue(entrust1.getShipper() != null ? entrust1.getShipper() : "");
            cell = row.createCell(3);
            cell.setCellStyle(cellStyles[3]);
            cell.setCellValue(entrust1.getConsignee() != null ? entrust1.getConsignee() : "");
            cell = row.createCell(4);
            cell.setCellStyle(cellStyles[4]);
            cell.setCellValue(entrust1.getNotifyParty() != null ? entrust1.getNotifyParty() : "");
            cell = row.createCell(5);
            cell.setCellStyle(cellStyles[5]);
            cell.setCellValue(entrust1.getLcNo() != null ? entrust1.getLcNo() : "");
            cell = row.createCell(6);
            cell.setCellStyle(cellStyles[6]);
            cell.setCellValue(entrust1.getPortOfLoading() != null ? entrust1.getPortOfLoading() : "");
            cell = row.createCell(7);
            cell.setCellStyle(cellStyles[7]);
            cell.setCellValue(entrust1.getPortOfTrans() != null ? entrust1.getPortOfTrans() : "");
            cell = row.createCell(8);
            cell.setCellStyle(cellStyles[8]);
            cell.setCellValue(entrust1.getPortOfDischar() != null ? entrust1.getPortOfDischar() : "");
            cell = row.createCell(9);
            cell.setCellStyle(cellStyles[9]);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
            cell.setCellValue(entrust1.getLoadingDate() != null ? simpleDateFormat.format(entrust1.getLoadingDate()) : "");
            cell = row.createCell(10);
            cell.setCellStyle(cellStyles[10]);
            cell.setCellValue(entrust1.getIsBatch() != null ? entrust1.getIsBatch() : "");
            cell = row.createCell(11);
            cell.setCellStyle(cellStyles[11]);
            cell.setCellValue(entrust1.getIsTrans() != null ? entrust1.getIsTrans() : "");
            cell = row.createCell(12);
            cell.setCellStyle(cellStyles[12]);
            cell.setCellValue(entrust1.getRemark() != null ? entrust1.getRemark() : "");
            cell = row.createCell(13);
            cell.setCellStyle(cellStyles[13]);
            if (entrust1.getState() == null) {
                cell.setCellValue("");
            } else {
                switch (entrust1.getState()) {
                    case 0:
                        cell.setCellValue("草稿");
                        break;
                    case 1:
                        cell.setCellValue("已经上报");
                        break;
                    case 2:
                        cell.setCellValue("已报运");
                        break;
                }
            }
        }

*/
if (entrust != null && entrust.size() > 0) {

        for (int i = 0; i < entrust.size(); i++) {
            //取出每个数据
            Entrust entrust1 = entrust.get(i);
            XSSFRow row = sheet.createRow(2 + i);
            //设置行高
            row.setHeightInPoints(24);
            //获取单元格
            //设置里面的每个值
            try {
                cell = row.createCell(0);
                cell.setCellStyle(cellStyles[0]);
                cell.setCellValue(entrust1.getShippingOrderId());

                cell = row.createCell(1);
                cell.setCellStyle(cellStyles[1]);
                cell.setCellValue(entrust1.getOrderType());
                cell = row.createCell(2);
                cell.setCellStyle(cellStyles[2]);
                cell.setCellValue(entrust1.getShipper());
                cell = row.createCell(3);
                cell.setCellStyle(cellStyles[3]);
                cell.setCellValue(entrust1.getConsignee());
                cell = row.createCell(4);
                cell.setCellStyle(cellStyles[4]);
                cell.setCellValue(entrust1.getNotifyParty());
                cell = row.createCell(5);
                cell.setCellStyle(cellStyles[5]);
                cell.setCellValue(entrust1.getLcNo());
                cell = row.createCell(6);
                cell.setCellStyle(cellStyles[6]);
                cell.setCellValue(entrust1.getPortOfLoading());
                cell = row.createCell(7);
                cell.setCellStyle(cellStyles[7]);
                cell.setCellValue(entrust1.getPortOfTrans());
                cell = row.createCell(8);
                cell.setCellStyle(cellStyles[8]);
                cell.setCellValue(entrust1.getPortOfDischar());
                cell = row.createCell(9);
                cell.setCellStyle(cellStyles[9]);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd");
                cell.setCellValue(simpleDateFormat.format(entrust1.getLoadingDate()));
                cell = row.createCell(10);
                cell.setCellStyle(cellStyles[10]);
                cell.setCellValue(entrust1.getIsBatch());
                cell = row.createCell(11);
                cell.setCellStyle(cellStyles[11]);
                cell.setCellValue(entrust1.getIsTrans());
                cell = row.createCell(12);
                cell.setCellStyle(cellStyles[12]);
                cell.setCellValue(entrust1.getRemark());
                cell = row.createCell(13);
                cell.setCellStyle(cellStyles[13]);
                switch (entrust1.getState()) {
                    case 0:
                        cell.setCellValue("草稿");
                        break;
                    case 1:
                        cell.setCellValue("已经上报");
                        break;
                    case 2:
                        cell.setCellValue("已报运");
                        break;
                }
            }catch (Exception e){
                cell.setCellValue("");
            }
        }


        //7.把工作簿作为文件流写出给用户(response)
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //把工作簿字节数据写入到ByteArrayOutputStream流
        workbook.write(byteArrayOutputStream);
        new DownloadUtil().download(byteArrayOutputStream,response,"出货表.xlsx");
    }
}

}

  • 写回答

2条回答 默认 最新

  • threenewbee 2020-08-28 11:26
    关注

    这取决于你的代码偏向容错性还是可靠性。这根据你的需要来,而没有一致的标准。
    容错性高的代码,可以忽略无关的错误,尽可能给用户一个结果。
    比如用户调查表,用户少写一个无关紧要答案,就把所有结果全部作废,会让用户觉得沮丧。
    可靠性高的代码,杜绝给出状态不明,无效的结果,但是造成用户的不便。
    比如开具一张发票,你必须每个项都准确无误,而不是开出废票,反倒给用户添乱。
    当然,对于容错性的设计,也要尽可能告诉用户,存在哪些潜在的错误。举例来说,早期的开发工具,比如vb6,当用户输错误的代码的时候,马上就弹出个框,很烦人。
    现在的开发工具,当发现用户输入错误,给出一个波浪线提示,但是不打扰用户,这就很好。
    你导出excel,可以在遇到错误的时候,把错误信息输出出来,或者在表格中写入,此列无法计算。比留一个空白更好。引起用户的注意,但是不过度打扰用户。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式