T_Customer[] customer = T_CustomerBLL.GetAll();
HSSFWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet("客户信息(" + DateTime.Now.ToString("yyyy-MM-dd") + ")");
IRow headerRow = sheet.CreateRow(0);
headerRow.CreateCell(0, CellType.String).SetCellValue("姓名");
headerRow.CreateCell(1, CellType.String).SetCellValue("电话");
headerRow.CreateCell(2, CellType.String).SetCellValue("手机");
headerRow.CreateCell(3, CellType.String).SetCellValue("邮编");
headerRow.CreateCell(4, CellType.String).SetCellValue("地址");
headerRow.CreateCell(5, CellType.String).SetCellValue("门店编号");
headerRow.CreateCell(6, CellType.String).SetCellValue("购买日期");
表头直接用字符串的就会乱码,还有sheet的名称直接用字符串也会乱码
for (int i = 0; i < customer.Length; i++)
{
IRow row = sheet.CreateRow(i + 1);
row.CreateCell(0, CellType.String).SetCellValue(customer[i].c_Name);
row.CreateCell(1, CellType.String).SetCellValue(customer[i].c_TelPhone);
row.CreateCell(2, CellType.String).SetCellValue(customer[i].c_MobilePhone);
row.CreateCell(3, CellType.String).SetCellValue(customer[i].c_Post);
row.CreateCell(4, CellType.String).SetCellValue(customer[i].c_Address);
row.CreateCell(5, CellType.String).SetCellValue(customer[i].c_ShopNum);
row.CreateCell(6, CellType.String).SetCellValue(Convert.ToString( customer[i].c_BuyDate.ToString("yyyy-MM-dd")));
row.CreateCell(7, CellType.String).SetCellValue(customer[i].c_CarNum);
row.CreateCell(8, CellType.String).SetCellValue(customer[i].c_BracketNum);
row.CreateCell(9, CellType.String).SetCellValue(customer[i].c_Brand);
row.CreateCell(10, CellType.String).SetCellValue(customer[i].c_TypeNum);
row.CreateCell(11, CellType.String).SetCellValue(customer[i].c_Suggestion);
row.CreateCell(12, CellType.String).SetCellValue(customer[i].c_Remarks);
//设置自动列宽度
for (int t = 0; t < row.LastCellNum; t++)
{
sheet.AutoSizeColumn(t);
}
}
using (FileStream stream = File.OpenWrite(filePath))
{
workbook.Write(stream);
}
下面的在数据库查出来的数据在Excel里面就不会乱码
反正就是直接写用双引号括住的字符就会乱码。
这是怎么回事
我用的是NPOI binary 2.1.3.1
字符编解码的问题吧。
这样试试:
string name = Encoding.UTF8.GetString(Encoding.Default.GetBytes("姓名"));这是转utf8的吧,
但是还是乱码,只是跟之前的乱码不一样之前是
ÐÕÃû
现在是 ?DD 乱七八糟的