导入excel问题

永动bug制造机 2011-09-22 11:17:29

DataTable dt = new DeceasedManage().BookDetail_Query(from, to);
int rows = dt.Rows.Count;//行数
int columns = dt.Columns.Count;//列数


Workbook book = new Workbook();
Worksheet sheet = new Worksheet("sheet1");


//设置标题
sheet.Cells[0, 0] = new Cell("登记表");

//设置列名
for (int i = 0; i < columns; i++)
{
sheet.Cells[1, i] = new Cell(dt.Columns[i].ColumnName);
}

//导入值
string value = "";
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < columns; c++)
{
value = dt.Rows[r][c].ToString();
sheet.Cells[r + 2, c] = new Cell(value);
}
}

book.Worksheets.Add(sheet);
string name = DateTime.Now.ToString("yyyy-MM-dd hh.mm.ss");
book.Save(@"E:\" + name + ".xls");

在Save的时候有异常,异常如下:
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------

指定的参数已超出有效值的范围。
参数名: SATSectorIndex
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: SATSectorIndex

源错误:

执行当前 Web 请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。

堆栈跟踪:


[ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: SATSectorIndex]
ExcelLibrary.Office.CompoundDocumentFormat.MasterSectorAllocation.GetSATSectorID(Int32 SATSectorIndex) +183
ExcelLibrary.Office.CompoundDocumentFormat.SectorAllocation.LinkSectorID(Int32 sectorID, Int32 newSectorID) +164
ExcelLibrary.Office.CompoundDocumentFormat.MasterSectorAllocation.AllocateMSATSector() +242
ExcelLibrary.Office.CompoundDocumentFormat.MasterSectorAllocation.AllocateSATSector() +316
ExcelLibrary.Office.CompoundDocumentFormat.MasterSectorAllocation.GetSATSectorID(Int32 SATSectorIndex) +124
ExcelLibrary.Office.CompoundDocumentFormat.SectorAllocation.LinkSectorID(Int32 sectorID, Int32 newSectorID) +164
ExcelLibrary.Office.CompoundDocumentFormat.MasterSectorAllocation.AllocateMSATSector() +242
ExcelLibrary.Office.CompoundDocumentFormat.MasterSectorAllocation.AllocateSATSector() +316
ExcelLibrary.Office.CompoundDocumentFormat.MasterSectorAllocation.GetSATSectorID(Int32 SATSectorIndex) +124
...全文
160 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
star 2012-09-29
  • 打赏
  • 举报
回复
请问楼主这问题解决了没?我也遇到了这个问题,是有行数限制的,2003版的为65535,但也不至于3W就出错呀
永动bug制造机 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lympc 的回复:]
应为 book.Save(@"E:\" + name + ".xls"); 这句应该没错
@在c#中为强制不转义 的符号,在里面的转义字符无效
string str1 = "abc\n"; // 这里\n当然默认转为换行符
string str2 = @"abc\n" // 这里\n不自动转为换行符号,相当于 str2 = "abc\\n"


这还真有点不明白

试试分段保存的……
[/Quote]
分段是没问题的 说越界也有点牵强 难道导出的有行数限制?
LYMPC 2011-09-24
  • 打赏
  • 举报
回复
应为 book.Save(@"E:\" + name + ".xls"); 这句应该没错
@在c#中为强制不转义 的符号,在里面的转义字符无效
string str1 = "abc\n"; // 这里\n当然默认转为换行符
string str2 = @"abc\n" // 这里\n不自动转为换行符号,相当于 str2 = "abc\\n"


这还真有点不明白

试试分段保存的方法是否成功 ,2W一段


wquanchao 2011-09-24
  • 打赏
  • 举报
回复
没啥好说的,最后一行的索引越界了
wquanchao 2011-09-24
  • 打赏
  • 举报
回复
http://blog.csdn.net/leolws/article/details/5591185
永动bug制造机 2011-09-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lympc 的回复:]
book.Save(@"E:\\" + name + ".xls");
请使用\\,"\"是转义字符
[/Quote]
请问@的作用是什么呢。
我这里描述有误 是在行数大于3w+的时候才出现的错误。
LYMPC 2011-09-24
  • 打赏
  • 举报
回复
book.Save(@"E:\\" + name + ".xls");
请使用\\,"\"是转义字符
shihaojie1219 2011-09-24
  • 打赏
  • 举报
回复
看看是不是权限问题,修改下权限 或用SaveAs()试试

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧