导出excel---Demo

/**
 * 导出/入料件与企业版本号关系表
 * @param request
 * @param response
 * @param doExportCopyRightExcel
 */
@Override
public void getDoExportCopyRightExcelInformationApi(HttpServletRequest request, HttpServletResponse response, DoExportCopyRightExcel doExportCopyRightExcel) {
    InputStream is=null;
    ByteArrayOutputStream os=null;
    ServletOutputStream out1=null;
    try{
        List<DoExportCopyFeignDTO> data = exportDomainSecortDevelopOra8FeignClient.doExportCopyRightExcelInformationApi(doExportCopyRightExcel).getData();
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFSheet hssfSheet = hssfWorkbook.createSheet();
        HSSFRow row = hssfSheet.createRow(0);
        row.createCell(0).setCellValue("账套编号");
        row.createCell(1).setCellValue("成品内部货号编号");
        row.createCell(2).setCellValue("成品内部货号");
        row.createCell(3).setCellValue("成品货号描述");
        row.createCell(4).setCellValue("料件货号编号");


        hssfSheet.setColumnWidth(0,20*256);
        hssfSheet.setColumnWidth(1,20*256);
        hssfSheet.setColumnWidth(2,20*256);
        hssfSheet.setColumnWidth(3,20*256);
        hssfSheet.setColumnWidth(4,20*256);
int i = 1;
        for(DoExportCopyFeignDTO cdfListDto:data){
            HSSFRow hssfRow = hssfSheet.createRow(i++);

            hssfRow.createCell(0).setCellValue(cdfListDto.getIncn());
            if (cdfListDto.getSitm()==null){
                hssfRow.createCell(1).setCellValue(cdfListDto.getSitm());
            }else {
                hssfRow.createCell(1).setCellValue(cdfListDto.getSitm().trim());
            }
            if (cdfListDto.getVern()==null){
                hssfRow.createCell(2).setCellValue(cdfListDto.getVern());
            }else {
                hssfRow.createCell(2).setCellValue(cdfListDto.getVern().trim());
            }
            if (cdfListDto.getLogn1()==null){
                hssfRow.createCell(3).setCellValue(cdfListDto.getLogn1());
            }else {
                hssfRow.createCell(3).setCellValue(cdfListDto.getLogn1().trim());
            }
            if (cdfListDto.getDate1()==null){
                hssfRow.createCell(4).setCellValue(cdfListDto.getDate1());
            }else {
                hssfRow.createCell(4).setCellValue(cdfListDto.getDate1().trim());
            }
            }
        os = new ByteArrayOutputStream();
        hssfWorkbook.write(os);
        byte[] content = os.toByteArray();
        is = new ByteArrayInputStream(content);
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename="+ new String(("货号料件企业版本号.xls").getBytes(), "UTF-8"));
        response.setHeader("Access-Control-Allow-Origin","*");
        out1 = response.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out1);
            byte[] buff = new byte[2048];
            int bytesRead;
            // Simple read/write loop.
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
        }
        catch (Exception e){
        e.printStackTrace();
    }finally {
        try {
            if (is != null) {
                is.close();
            }
            if (os != null) {
                os.close();
            }
            if (out1 != null) {
                out1.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
Excel导入导出Excel软件中非常常见且重要的功能之一。在实际应用开发中,我们常常需要通过程序来实现对Excel文件的导入和导出操作。下面是一个示例的Excel导入导出Demo源码。 导入功能部分: ```java import java.io.FileInputStream; import java.io.InputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelImportDemo { public static void main(String[] args) { try { // 打开要导入的Excel文件 InputStream is = new FileInputStream("导入文件路径"); Workbook workbook = new XSSFWorkbook(is); Sheet sheet = workbook.getSheetAt(0); // 遍历每一行数据 for (Row row : sheet) { // 遍历每个单元格 for (Cell cell : row) { // 将单元格内容输出 System.out.print(cell.getStringCellValue() + "\t"); } System.out.println(); } // 关闭流 workbook.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 导出功能部分: ```java import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelExportDemo { public static void main(String[] args) { try { // 创建Excel工作簿 Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Sheet1"); // 创建数据行并填充数据 for (int i = 0; i < 5; i++) { Row row = sheet.createRow(i); for (int j = 0; j < 5; j++) { Cell cell = row.createCell(j); cell.setCellValue("数据" + (i + 1) + "-" + (j + 1)); } } // 保存Excel文件 FileOutputStream fos = new FileOutputStream("导出文件路径"); workbook.write(fos); workbook.close(); fos.close(); System.out.println("导出成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 以上是一个简单的Excel导入导出Demo源码,可以帮助你了解如何通过Java代码实现Excel文件的导入和导出功能。你可以根据实际需要进行修改和扩展,以适应更复杂的导入导出操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值