Kendo Grid导出到Excel“输入字符串的格式不正确”

时间:2014-08-15 17:19:55

标签: asp.net-mvc entity-framework kendo-grid export-to-excel

我使用了实体框架,我正在尝试导出到excel。它一直告诉我输入字符串不正确。我把它们全部转换为字符串,我无法弄清楚它不喜欢哪一个。它可能是标题行吗?如果是这样,我该如何解决这个问题呢?

 private BaselineDataEntities db = new BaselineDataEntities();
    public FileResult Export([DataSourceRequest]DataSourceRequest request)
    {
        //Get the data representing the current grid state - page, sort and filter
        IEnumerable products = db.ProductQualityFileFulls.ToDataSourceResult(request).Data;

        //Create new Excel workbook
        var workbook = new HSSFWorkbook();

        //Create new Excel sheet
        var sheet = workbook.CreateSheet();


       // //Create a header row
        var headerRow = sheet.CreateRow(0);

       //// Set the column names in the header row
        headerRow.CreateCell(0).SetCellValue("Recent Update Status");
        headerRow.CreateCell(1).SetCellValue("Marketing Rep");
        headerRow.CreateCell(2).SetCellValue("Customer Name");
        headerRow.CreateCell(3).SetCellValue("Opt-In Status");
        headerRow.CreateCell(4).SetCellValue("Store ID");
        headerRow.CreateCell(5).SetCellValue("Dealer");
        headerRow.CreateCell(6).SetCellValue("DBA Name");
        headerRow.CreateCell(7).SetCellValue("Class");
        headerRow.CreateCell(8).SetCellValue("Act Code Desc");
        headerRow.CreateCell(9).SetCellValue("Jobber");
        headerRow.CreateCell(10).SetCellValue("Jobber Name");
        headerRow.CreateCell(11).SetCellValue("SubJobber");
        headerRow.CreateCell(12).SetCellValue("Company Op Desc");
        headerRow.CreateCell(13).SetCellValue("Longitude");
        headerRow.CreateCell(14).SetCellValue("Latitude");
        headerRow.CreateCell(15).SetCellValue("Local Address");
        headerRow.CreateCell(16).SetCellValue("Local Address 2");
        headerRow.CreateCell(17).SetCellValue("Local City");
        headerRow.CreateCell(18).SetCellValue("Local County");
        headerRow.CreateCell(19).SetCellValue("Local State");
        headerRow.CreateCell(20).SetCellValue("Local Postal");
        headerRow.CreateCell(21).SetCellValue("Phone");
        headerRow.CreateCell(22).SetCellValue("Establish Date");
        headerRow.CreateCell(23).SetCellValue("Establish ID"); 

       //// (Optional) freeze the header row so it is not scrolled
       // sheet.CreateFreezePane(0, 1, 0, 1);

        int rowNumber = 1;

        //Populate the sheet with values from the grid data
        foreach (ProductQualityFileFull product in products)
        {
            //Create a new row
            var row = sheet.CreateRow(rowNumber++);

            //Set values for the cells
            row.CreateCell(0).SetCellValue(product.RecUpdtStatus.ToString());
            row.CreateCell(1).SetCellValue(product.mktRep.ToString());
            row.CreateCell(2).SetCellValue(product.CustName.ToString());
            row.CreateCell(3).SetCellValue(product.OptInd.ToString());
            row.CreateCell(4).SetCellValue(product.StoreId.ToString());
            row.CreateCell(5).SetCellValue(product.dealerNo.ToString());
            row.CreateCell(6).SetCellValue(product.DBAName.ToString());
            row.CreateCell(7).SetCellValue(product.Class.ToString());
            row.CreateCell(8).SetCellValue(product.ActCodedesc.ToString());
            row.CreateCell(9).SetCellValue(product.Jobber.ToString());
            row.CreateCell(10).SetCellValue(product.JobberName.ToString());
            row.CreateCell(11).SetCellValue(product.SubJobber.ToString());
            row.CreateCell(12).SetCellValue(product.CompanyOpDesc.ToString());
            row.CreateCell(13).SetCellValue(product.Longitude.ToString());
            row.CreateCell(14).SetCellValue(product.Latitude.ToString());
            row.CreateCell(15).SetCellValue(product.LocAddress.ToString());
            row.CreateCell(16).SetCellValue(product.LocAddress2.ToString());
            row.CreateCell(17).SetCellValue(product.LocCity.ToString());
            row.CreateCell(18).SetCellValue(product.LocCounty.ToString());
            row.CreateCell(19).SetCellValue(product.LocState.ToString());
            row.CreateCell(20).SetCellValue(product.LocPostalCode.ToString());
            row.CreateCell(21).SetCellValue(product.Phone.ToString());
            row.CreateCell(22).SetCellValue(product.establish_date_time.ToString());
            row.CreateCell(23).SetCellValue(product.establish_id.ToString());
        }

       // Write the workbook to a memory stream
        MemoryStream output = new MemoryStream();
        workbook.Write(output);

        //eturn the result to the end user

        return File(output.ToArray(),   //The binary data of the XLS file
            "application/vnd.ms-excel", //MIME type of Excel files
            "GridExcelExport.xls");     //Suggested file name in the "Save as" dialog which will be displayed to the end user

    }

1 个答案:

答案 0 :(得分:0)

你有没有尝试过评论内容并逐一建立起来。 在不知道数据是如何构建的情况下,很难说出你的问题是什么,如果你突破代码,你应该能够找出问题所在(或调试语句)

当您使用npoi时,这个小图书馆可能对您有用:

Excel Helper

这是我构建的一个项目,它可以轻松地将展平列表集合转换为xls和xlsx文件类型的excel文件。它纯粹只是为了获取数据,而不是做任何花哨的图形,但对于将数据放入工作表非常有用。