附加=“0xxxx”使用 NPOI 导出时使用 Excel 单元格来保持前导零,或者如何将单元格设置为文本

发布于 2024-12-18 11:59:18 字数 3370 浏览 4 评论 0原文

我试图在 asp.net mvc 3 应用程序中使用 NPOI 导出保存电话号码的列以保持前导零。我读过这里; 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 方法,但没有成功。

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

I am trying to maintain the leading zeros while exporting a column holding phone numbers to excel using NPOI in an asp.net mvc 3 application. I have read here; http://creativyst.com/Doc/Articles/CSV/CSV01.htm#CSVAndExcel that I can append ="[some number beginning with 0]" to have excel maintain the zeros. I have also read that if I set the cell as text it will maintain the zero. I have been unsuccessful in my attempts to do this. Here is my code;

 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
    }
}

I have tried the setCellTyoe method in various places with no luck.

I don't mind how it's done, I just want to maintain the leading zeros when the sheet is exported.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

公布 2024-12-25 11:59:18

我无法测试这一点,但您是否尝试在 createCell 调用?如果所有其他方法都失败了,您可以求助于在前导零之前放置单引号的尝试和真正的技巧:

'00185

这将强制单元格为文本,并且 Excel 应该仅在编辑时显示它。

I can not test this but did you try setting the type in the createCell call? If all else fails you could resort to the tried and true hack of putting a single quote before the leading zero:

'00185

This will force the cell to text and excel should only display it on edit.

孤芳又自赏 2024-12-25 11:59:18

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

Changed the data type from string to int and NPOI set the cell as text, keeping the leading zero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文