private void ExportExcel()
{
DataTable dtVilationList = Grab.GetVilationList(guid.ToString()); SaveFileDialog dlg = new SaveFileDialog()
{
Filter = "*.xls|*.xls",
RestoreDirectory = true,
FileName = DateTime.Now.ToString("yyyyMMddhhmmss")
};
if (dlg.ShowDialog() == DialogResult.OK)
{
string[] columns = new string[] {
"车牌","违规时间","违规行为","违规地点","罚款金额","扣分","异地办理费"
};
int i = 0;
foreach (string c in columns)
{
dtVilationList.Columns[i++].ColumnName = c;
}
CSD.Tools.DownExcel downExcel = new CSD.Tools.DownExcel();
downExcel.Width(0, 16);
downExcel.Width(1, 18);
downExcel.Width(2, 35);
downExcel.Width(3, 35);
downExcel.Width(6, 16);
downExcel.File(dtVilationList, dlg.FileName);
MessageBox.Show("导出成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
第一次SaveFileDialog 是正常显示,并能保存,,再次点击导出Excel的话会报错误,
错误定位在,“ if (dlg.ShowDialog() == DialogResult.OK)”
错误信息:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
数据是从Access读取的
{
DataTable dtVilationList = Grab.GetVilationList(guid.ToString()); SaveFileDialog dlg = new SaveFileDialog()
{
Filter = "*.xls|*.xls",
RestoreDirectory = true,
FileName = DateTime.Now.ToString("yyyyMMddhhmmss")
};
if (dlg.ShowDialog() == DialogResult.OK)
{
string[] columns = new string[] {
"车牌","违规时间","违规行为","违规地点","罚款金额","扣分","异地办理费"
};
int i = 0;
foreach (string c in columns)
{
dtVilationList.Columns[i++].ColumnName = c;
}
CSD.Tools.DownExcel downExcel = new CSD.Tools.DownExcel();
downExcel.Width(0, 16);
downExcel.Width(1, 18);
downExcel.Width(2, 35);
downExcel.Width(3, 35);
downExcel.Width(6, 16);
downExcel.File(dtVilationList, dlg.FileName);
MessageBox.Show("导出成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
第一次SaveFileDialog 是正常显示,并能保存,,再次点击导出Excel的话会报错误,
错误定位在,“ if (dlg.ShowDialog() == DialogResult.OK)”
错误信息:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
数据是从Access读取的
解决方案 »
- 如何遍历GridControl控件中gridview列
- C#委托实现刷新页面
- 求救:c# 传递给系统调用的数据区域太小。 (异常来自 HRESULT:0x8007007A)
- 数据库连接问题,请高手帮忙,非常感谢
- 如何卸载SQL SERVER2005?
- 关于remoting的客户端的存在与否?!
- C#winform如何在txt里剪切一行或进行一行一行的剪切;或删除一行?
- 设计一个多功能计算器,急求在线坐等!!!!!
- 关于DataGrid的问题
- 请问:有谁在C#中用过Remoting???
- 谁有javascript和ajax比较好的教程啊?
- WebBrowser.Document.Body.OuerHtml 乱码
{
Filter = "*.xls|*.xls",
RestoreDirectory = true,
FileName = DateTime.Now.ToString("yyyyMMddhhmmss")
};
if (dlg.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("导出成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
File()方法是这样写的:
/// <summary>
/// 输出数据
/// </summary>
/// <param name="FileName"></param>
public void File(string FileName)
{
using(MemoryStream ms = new MemoryStream())
{
hssfworkbook.Write(ms);
ms.Flush();
ms.Position = 0;
using (FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write))
{
byte[] data = ms.ToArray();
fs.Write(data, 0, data.Length);
fs.Flush();
}
}
}
这个应该和SaveFileDialog没多大关系吧
DataTable dtVilationList = Grab.GetVilationList(guid.ToString());
其中Grab是个抽象类,我调试发现是这个地方的问题
如果DataTable dtVilationList = Grab.GetVilationList(guid.ToString());只初始化一次,就没问题