问题:未能加载文件或程序集“ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf116
解决方案:
1:发现没有引用此dll,在项目的package文件夹下SharpZipLib.0.86.0文件夹中找到SharpZipLib.dll引用,即OK。
2:引用版本不对。
问题:System.ObjectDisposedException: 无法访问已关闭的文件。(顺便附上源代码)
/// <summary>
/// 导出数据到Excel中
/// </summary>
/// <param name="Url">文件导出地址</param>
public void Export(string Url, List<Word> list)
{
try
{
//创建Excel文件的对象
IWorkbook book;
string fileExt = Path.GetExtension(Url).ToLower();
if (fileExt == ".xlsx")
{
book = new XSSFWorkbook();
}
else if (fileExt == ".xls")
{
book = new HSSFWorkbook();
}
else
{
book = null;
}
if (book == null)
{
return;
}
//添加一个sheet
ISheet sheetWords = book.CreateSheet("Words");
//给sheet添加第一行的头部标题
IRow rowWords = sheetWords.CreateRow(0);
rowWords.CreateCell(0).SetCellValue("序号");
rowWords.CreateCell(1).SetCellValue("词条");
rowWords.CreateCell(2).SetCellValue("词性词义");
//将数据逐步写入sheet各个行
for (int i = 0, k = 0; i < list.Count - 1; i++, k++)
{
IRow rowtemps = sheetWords.CreateRow(k + 1);
rowtemps.CreateCell(0).SetCellValue(k + 1);
rowtemps.CreateCell(1).SetCellValue(list[i + 1].WordEntry);
rowtemps.CreateCell(2).SetCellValue(list[i + 1].WordProperty + list[i + 1].Meaning);
}
// 写入到文件
FileStream fs = new FileStream(Url, FileMode.Create);
book.Write(fs);
fs.Seek(0, SeekOrigin.Begin);
fs.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message.ToString()+"\t"+ex.ToString());
}
}
解决方法:注释了fs.Seek(0, SeekOrigin.Begin);就可了!