0

我试图在 asp.net mvc 3 应用程序中使用 NPOI 将保存电话号码的列导出到 excel 时保持前导零。我在这里读过;http://creativyst.com/Doc/Articles/CSV/CSV01.htm#CSVAndExcel我可以附加 ="[一些以 0 开头的数字]" 让 excel 保持零。我还读过,如果我将单元格设置为文本,它将保持为零。我尝试这样做没有成功。这是我的代码;

 public ActionResult Export(int page, string orderBy, string filter)
    {
        //Get the data representing the current grid state - page, sort and filter
        GridModel model = Model().ToGridModel(page, 10, orderBy, string.Empty, filter);
        var orders = model.Data.Cast<Advertiser>();

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


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


        //(Optional) set the width of the columns
        sheet.SetColumnWidth(0, 10 * 256);
        sheet.SetColumnWidth(1, 50 * 256);
        sheet.SetColumnWidth(2, 50 * 256);
        sheet.SetColumnWidth(3, 50 * 256);

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



        //Set the column names in the header row
        headerRow.CreateCell(0).SetCellValue("Name");
        headerRow.CreateCell(1).SetCellValue("Phone");
        headerRow.CreateCell(5).SetCellValue("Company Name");
        headerRow.CreateCell(7).SetCellValue("Address 1");
        headerRow.CreateCell(8).SetCellValue("Address 2");
        headerRow.CreateCell(9).SetCellValue("Address 3");
        headerRow.CreateCell(10).SetCellValue("Address 4");
        headerRow.CreateCell(11).SetCellValue("Post Code");
        headerRow.CreateCell(14).SetCellValue("Email");
        headerRow.CreateCell(16).SetCellValue("Website");
        headerRow.CreateCell(19).SetCellValue("Listing Type");

        //(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 (Advertiser order in orders)
        {
            //Create a new row
            var row = sheet.CreateRow(rowNumber++);

            //Set values for the cells
            row.CreateCell(0).SetCellValue(order.AdvertiserName);
            row.CreateCell(1).SetCellValue(order.Phone);
            row.CreateCell(3).SetCellValue(order.CompanyName);
            row.CreateCell(5).SetCellValue(order.Address1);
            row.CreateCell(6).SetCellValue(order.Address2);
            row.CreateCell(7).SetCellValue(order.Address3);
            row.CreateCell(8).SetCellValue(order.Address4);
            row.CreateCell(9).SetCellValue(order.Postcode);
            row.CreateCell(10).SetCellValue(order.AdvertiserEmail);
            row.CreateCell(11).SetCellValue(order.Website);
            row.CreateCell(12).SetCellValue(order.listing.type);



        }

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

        //Return 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
            "Advertisers.xls");     //Suggested file name in the "Save as" dialog which will be displayed to the end user
    }
}

我在各个地方都尝试过 setCellTyoe 方法,但没有成功。

我不介意它是如何完成的,我只想在导出工作表时保持前导零。

4

2 回答 2

1

我无法对此进行测试,但您是否尝试在createCell调用中设置类型?如果所有其他方法都失败了,您可以求助于在前导零之前放置单引号的久经考验的真正技巧:

'00185

这将强制单元格为文本,excel 应该只在编辑时显示它。

于 2011-11-27T15:12:08.037 回答
0

将数据类型从字符串更改为 int,NPOI 将单元格设置为文本,保持前导零。

于 2011-11-28T09:43:39.497 回答