【问题标题】:Error in opening an mail attached excel file created using Apache POI打开使用 Apache POI 创建的附加邮件的 Excel 文件时出错
【发布时间】:2014-06-25 09:32:21
【问题描述】:

我正在使用 Apache POI 创建一个 Excel 表,然后使用 Java GWT 发送相同的文件。创建的文件没问题。现在在邮件中有两个选项 - 保存或打开。当我将文件保存在机器中时工作正常,但是当我尝试打开时,它会在记事本中打开。在建议中它也没有显示 excel。这是我创建 excel 工作表的代码:

package com.ericsson.egi.sxs.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import com.ericsson.egi.sxs.persistence.entity.AssetOrder;

public class CreateExcelFile {

    int rownum = 0;
    HSSFSheet firstSheet;
    Collection<File> files;
    HSSFWorkbook workbook;

     CreateExcelFile() {
    }

    public File createWeeklyReport(List<AssetOrder> tempOrderList) throws Exception {
            workbook = new HSSFWorkbook();
            firstSheet = workbook.createSheet("FIRST SHEET");
            List<String> headerRow = new ArrayList<String>();
            headerRow.add("Name of Supplier/Vendor");
            headerRow.add("Description of Contract");
            headerRow.add("Initiator");
            headerRow.add("Type Of Contract");
            headerRow.add("Template Source");
            headerRow.add("Savings");
            headerRow.add("Payment Term");
            headerRow.add("Code of Conduct Signed by Supplier");
            headerRow.add("RoHS clause Included");
            headerRow.add("Agreement No");
            headerRow.add("Agreement Validity From ");
            headerRow.add("Agreement Validity To");
            headerRow.add("Sanctioned Parties List Screening");
            headerRow.add("Sanctioned Parties List Screening Reasons in case no answer NO");
            headerRow.add("Registered in CLM");
            headerRow.add("Registered in CLM reasons if answer NO");
            headerRow.add("Current State");
            headerRow.add("Next State");
            headerRow.add("TAT for L1");
            headerRow.add("TAT for L2");
            headerRow.add("TAT for L3");
            headerRow.add("TAT for L4");
            headerRow.add("Current State Comments");

            List<List> recordToAdd = new ArrayList<List>();
            recordToAdd.add(headerRow); 
            for (AssetOrder order : tempOrderList ) {
                List<String> row = new ArrayList<String>();
                row.add(order.getSourcingDetails().getVendorName());
                row.add(order.getSourcingDetails().getContractDescription());
                row.add(order.getSourcingDetails().getInitiatorName());
                row.add(order.getSourcingDetails().getContractType());
                row.add(order.getSourcingDetails().getTemplateSource()); 
                row.add(order.getSourcingDetails().getSavings());
                row.add(order.getSourcingDetails().getPaymentTerm()); 
                if (order.getSourcingDetails().getIsCOCSigned()) {
                    row.add("YES");
                } else {
                    row.add("NO");
                }

                if (order.getSourcingDetails().getIsROHSIncluded()) {
                    row.add("YES");
                } else {
                    row.add("NO"); 
                }
                row.add(order.getSourcingDetails().getAgreementNo());
                row.add(order.getSourcingDetails().getValidityFrom().toString()); 
                row.add(order.getSourcingDetails().getValidityTo().toString());
                if (order.getSourcingDetails().getIsSPLScreening()) {
                    row.add("YES");
                } else {
                    row.add("NO");
                }
                row.add(order.getSourcingDetails().getReasonsForSPL());
                if (order.getSourcingDetails().getIsRegisteredInCLM()) {
                    row.add("YES");
                } else {
                    row.add("NO");
                }
                row.add(order.getSourcingDetails().getReasonsForCLM());
                row.add(order.getStatusMaster().getStatusName()); 
                row.add(null);
                row.add(null); 
                row.add(null);
                row.add(null); 
                row.add(null);
                row.add(order.getComments()); 

                recordToAdd.add(row);
            }

            CreateExcelFile cls = new CreateExcelFile(recordToAdd);
            File file = cls.createExcelFile(tempOrderList.get(0).getOrderRequesterSignum());           
            return file;
        }

        File createExcelFile(String requesterSignum) {
            FileOutputStream fos = null;
            File file = new File("/tmp/" + requesterSignum + "_StatusReport.xls");
            try {

                fos=new FileOutputStream(file);
                HSSFCellStyle hsfstyle=workbook.createCellStyle();
                hsfstyle.setBorderBottom((short) 1);
                hsfstyle.setFillBackgroundColor((short)245);

                workbook.write(fos);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return file;
        }

         CreateExcelFile(List<List> l1) throws Exception {
         try {
                workbook = new HSSFWorkbook();
                firstSheet = workbook.createSheet("FIRST SHEET");
                for (int j = 0; j < l1.size(); j++) {
                    Row row = firstSheet.createRow(rownum);
                    List<String> l2= l1.get(j);

                    for(int k=0; k<l2.size(); k++)
                    {
                        Cell cell = row.createCell(k);
                        cell.setCellValue(l2.get(k));
                    }
                    rownum++;
                }

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }

        }

}

【问题讨论】:

  • 您的机器上安装了 Excel 吗?如果您尝试先启动 Excel 然后导航到该文件会发生什么情况?
  • 是的 Excel 已安装在我的机器中。当我下载附件时,它工作正常。当我尝试直接打开它时,它不会选择 excel。
  • 你在 Windows 上工作吗?
  • No.Linux。但是两个操作系统的问题都是一样的。
  • 问题是和setContentType有关吗?

标签: java gwt apache-poi


【解决方案1】:

根据您计算机上安装的 Excel 版本尝试使用其他 Excel Extension


--编辑--

问题可能与内容类型有关。请确认您使用的是什么?

response.setContentType("APPLICATION/OCTET-STREAM");

// try this one if above doesn't work
//response.setContentType("application/vnd.ms-excel");

response.setHeader("Content-disposition", "attachment;filename=myExcel.xls");

【讨论】:

  • 你设置了什么内容类型?
  • 我已经使用了内容类型来setContentType("application/vnd.ms-excel"); .当我将相同的邮件转发到我的 gmail 时,它的工作完全正常。那么问题出在 Outlook 上吗?
  • 我认为这是一个雷鸟邮件问题。因为同样适用于 gmail、yahoo 邮件或任何其他邮件
  • @Atul 抱歉,我对邮件 API 一无所知。你试过APPLICATION/OCTET-STREAM吗?
  • 是的,先生。它不起作用。实际上,我尝试了一个不同的 excel 文件,该文件与从雷鸟邮件中的附件打开的代码无关。但问题仍然存在。所以我得出的结论是什么都没有代码错误。感谢您的帮助:)祝您有美好的一天。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-07
相关资源
最近更新 更多