Asp.net mvc 3 附加=”号;0xxxx";使用NPOI导出时excel单元格以保持前导零,或如何将单元格设置为文本
我试图在asp.net mvc 3应用程序中使用NPOI将包含电话号码的列导出到excel时保持前导零。我在这里读过;我可以附加=“[some number start with 0]”让excel维护零。我还读到,如果我将单元格设置为文本,它将保持零。我尝试这样做失败了。这是我的密码Asp.net mvc 3 附加=”号;0xxxx";使用NPOI导出时excel单元格以保持前导零,或如何将单元格设置为文本,asp.net-mvc-3,export-to-excel,npoi,Asp.net Mvc 3,Export To Excel,Npoi,我试图在asp.net mvc 3应用程序中使用NPOI将包含电话号码的列导出到excel时保持前导零。我在这里读过;我可以附加=“[some number start with 0]”让excel维护零。我还读到,如果我将单元格设置为文本,它将保持零。我尝试这样做失败了。这是我的密码 public ActionResult Export(int page, string orderBy, string filter) { //Get the data represen
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
}
}
公共操作结果导出(int页、字符串orderBy、字符串筛选器)
{
//获取表示当前网格状态的数据-页面、排序和筛选
GridModel=model().ToGridModel(第10页,orderBy,string.Empty,filter);
var orders=model.Data.Cast();
//创建新的Excel工作簿
var工作簿=新的HSSF工作簿();
//创建新的Excel工作表
var sheet=workbook.CreateSheet();
//(可选)设置列的宽度
页。设置列宽(0,10*256);
页。设置柱宽(1,50*256);
页。设置柱宽(2,50*256);
页。设置柱宽(3,50*256);
//创建标题行
var headerRow=sheet.CreateRow(0);
//设置标题行中的列名
headerRow.CreateCell(0.SetCellValue(“名称”);
headerRow.CreateCell(1.SetCellValue(“电话”);
headerRow.CreateCell(5.SetCellValue(“公司名称”);
headerRow.CreateCell(7.SetCellValue(“地址1”);
headerRow.CreateCell(8.SetCellValue(“地址2”);
headerRow.CreateCell(9.SetCellValue(“地址3”);
headerRow.CreateCell(10.SetCellValue(“地址4”);
headerRow.CreateCell(11).SetCellValue(“邮政编码”);
headerRow.CreateCell(14).SetCellValue(“电子邮件”);
headerRow.CreateCell(16.SetCellValue(“网站”);
CreateCell(19).SetCellValue(“列表类型”);
//(可选)冻结标题行,使其不会滚动
创建冻结窗格(0,1,0,1);
int rowNumber=1;
//使用网格数据中的值填充图纸
foreach(广告主订单中的订单)
{
//创建新行
var row=sheet.CreateRow(rowNumber++);
//设置单元格的值
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(顺序.邮政编码);
row.CreateCell(10).SetCellValue(order.AdvertiserEmail);
CreateCell(11).SetCellValue(order.Website);
CreateCell(12).SetCellValue(order.listing.type);
}
//将工作簿写入内存流
MemoryStream输出=新的MemoryStream();
工作簿。编写(输出);
//将结果返回给最终用户
返回文件(output.ToArray(),//XLS文件的二进制数据
“application/vnd.ms excel”,//excel文件的MIME类型
“advisors.xls”);//将显示给最终用户的“另存为”对话框中的建议文件名
}
}
我在很多地方尝试过setCellTyoe方法,但都没有成功
我不介意它是如何完成的,我只想在导出工作表时保持前导零。我无法测试这一点,但您是否尝试在调用中设置类型?如果所有其他方法都失败了,你可以求助于一种久经考验的方法,在前导零之前加上一个引号:
'00185
这将强制单元格为文本,excel只能在编辑时显示。将数据类型从字符串更改为int,NPOI将单元格设置为文本,保留前导零。Hi Mark,感谢您的回复。我一直在尝试设置单元格类型,但失败了。这个建议和“黑客”的结合让我意识到,我可以尝试将模型中的属性类型从int改为string,看看这是否会有所不同。是的,现在npoi将该单元格视为文本并保留前导零。即使您自己回答了,您也应该接受答案,这样其他人就可以看到您的问题已得到解决:)谢谢,我必须等待18个小时才能完成,我忘了,谢谢您提醒我!