excel是空的,帮我看看是哪里的问题,拜谢!
代码:
using (MemoryStream ms = ListToExcel<GapInfoForExport>(gapInfo, propertyName))
{
SaveToFile(ms, fileName);
}
public static MemoryStream ListToExcel<T>(IList<T> list, params string[] propertyName)
{
//创建流对象
using (MemoryStream ms = new MemoryStream())
{
//将参数写入到一个临时集合中
List<string> propertyNameList = new List<string>();
if (propertyName != null)
propertyNameList.AddRange(propertyName);
//床NOPI的相关对象
IWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet();
IRow headerRow = sheet.CreateRow(0);
if (list.Count > 0)
{
//通过反射得到对象的属性集合
PropertyInfo[] propertys = list[0].GetType().GetProperties();
//遍历属性集合生成excel的表头标题
for (int i = 0; i < propertys.Count(); i++)
{
//判断此属性是否是用户定义属性
if (propertyNameList.Count == 0)
{
headerRow.CreateCell(i).SetCellValue(propertys[i].Name);
}
else
{
if (propertyNameList.Contains(propertys[i].Name))
headerRow.CreateCell(i).SetCellValue(propertys[i].Name);
}
}
int rowIndex = 1;
//遍历集合生成excel的行集数据
for (int i = 0; i < list.Count; i++)
{
IRow dataRow = sheet.CreateRow(rowIndex);
for (int j = 0; j < propertys.Count(); j++)
{
if (propertyNameList.Count == 0)
{
object obj = propertys[j].GetValue(list[i], null);
dataRow.CreateCell(j).SetCellValue(obj.ToString());
}
else
{
if (propertyNameList.Contains(propertys[j].Name))
{
object obj = propertys[j].GetValue(list[i], null);
dataRow.CreateCell(j).SetCellValue(obj.ToString());
}
}
}
rowIndex++;
}
}
workbook.Write(ms);
ms.Flush();
ms.Position = 0;
return ms;
}
}
public static void SaveToFile(MemoryStream ms, string fileName)
{
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
byte[] data = ms.ToArray();
fs.Write(data, 0, data.Length);
fs.Flush();
data = null;
}
}
你不会单步运行吗?
大叔,又是你呀,我没玩过这个不会呀,这个是仿造别人的,你教我吧
@没有同名:
你们学习ASP.net的时候,书上没有教你们学习一个叫控制台应用程序的东西?
你把这段代码放里面去,然后按F10,就一步步走给你看了啊....
哪一步对了,哪一步错了,都可以看到的,走到循环的时候,可以看循环次数是多少。
@爱编程的大叔: 大叔,现在是部分数据已经导出,但是有部分数据没有导出,好奇怪