程序开发完成后,安装到用户客户端,却发现某些计算机不能导出成Excel文件,提示错误信息如下:
System.Exception: error from export to excel ---> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at TTF160.F1BookClass.Write(String PathName, Int16 FileType)
经在有问题的电脑上调试后发现,是所有数据操作完成后,最后保存excel文件时报错,并且是只有选中excel方式出错,如选成txt, html方式都没有问题。 以下是代码,恳请帮忙。 # region Methods
private void AddTitle(int srow, int scol, int erow, int ecol)
{
....
} private void AddSubTitle(int srow, int scol, int erow, int ecol)
{
....
}
private void AddDataTitle(int row, int col, int[] colWidth)
{
....
} private void AddDataBody(int row, int col, int[] colWidth)
{
....
} private void FontBold(int srow, int scol, int erow, int ecol)
{
....
} private void FontName(int srow, int scol, int erow, int ecol)
{
....
} private void FontSize(int srow, int scol, int erow, int ecol)
{
....
} private void DataBoxBorder(int srow, int scol, int erow, int ecol)
{
....
} private void NumberDataFormat(int srow, int erow, int[] colWidth)
{
....
} private void SetColWidth(int[] colWidth)
{
....
} private void SetColAlign(int srow, int scol, int erow, int ecol)
{
....
} private void PageSetup(int titlerow)
{
.....
} private void Save()
{
// 1
m_Sheet.Write(m_FileName, (short)TTF160.F1FileTypeConstants.F1FileExcel97); // 2
//short filetype = 11;
//m_Sheet.SaveFileDlg("Save", out m_FileName, out filetype);
//m_Sheet.Write(m_FileName, filetype);
} # endregion public void BuildExcel(int[] colWidth, bool isSave)
{
int row = 1, col = 1, erow = 1, ecol = 0; if (colWidth.Length != m_dtReport.Columns.Count)
return;
else
foreach (DataColumn dc in m_dtReport.Columns)
{
if (colWidth[dc.Ordinal] > 0)
ecol++;
} try
{
// Title
erow = row + 2;
AddTitle(row, col, erow, ecol);
row = row + 3; // Sub-title
AddSubTitle(row, col, row, ecol);
row = row + 1; // Data title
AddDataTitle(row, col, colWidth);
row = row + 1; // Data
AddDataBody(row, col, colWidth); // Style
erow = m_dtReport.Rows.Count + row - 1;
FontName(row, col, erow, ecol);
FontSize(row, col, erow, ecol);
NumberDataFormat(row, erow, colWidth);
SetColWidth(colWidth); // autofit column
m_Sheet.SetColWidthAuto(1, 1, erow, ecol, true); // Page Setup
PageSetup(row - 1); // Save
if (isSave)
Save();
else
m_Sheet.FilePrintPreview();
}
catch (Exception ex)
{
throw new Exception("error from export to excel", ex);
}
}
System.Exception: error from export to excel ---> System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at TTF160.F1BookClass.Write(String PathName, Int16 FileType)
经在有问题的电脑上调试后发现,是所有数据操作完成后,最后保存excel文件时报错,并且是只有选中excel方式出错,如选成txt, html方式都没有问题。 以下是代码,恳请帮忙。 # region Methods
private void AddTitle(int srow, int scol, int erow, int ecol)
{
....
} private void AddSubTitle(int srow, int scol, int erow, int ecol)
{
....
}
private void AddDataTitle(int row, int col, int[] colWidth)
{
....
} private void AddDataBody(int row, int col, int[] colWidth)
{
....
} private void FontBold(int srow, int scol, int erow, int ecol)
{
....
} private void FontName(int srow, int scol, int erow, int ecol)
{
....
} private void FontSize(int srow, int scol, int erow, int ecol)
{
....
} private void DataBoxBorder(int srow, int scol, int erow, int ecol)
{
....
} private void NumberDataFormat(int srow, int erow, int[] colWidth)
{
....
} private void SetColWidth(int[] colWidth)
{
....
} private void SetColAlign(int srow, int scol, int erow, int ecol)
{
....
} private void PageSetup(int titlerow)
{
.....
} private void Save()
{
// 1
m_Sheet.Write(m_FileName, (short)TTF160.F1FileTypeConstants.F1FileExcel97); // 2
//short filetype = 11;
//m_Sheet.SaveFileDlg("Save", out m_FileName, out filetype);
//m_Sheet.Write(m_FileName, filetype);
} # endregion public void BuildExcel(int[] colWidth, bool isSave)
{
int row = 1, col = 1, erow = 1, ecol = 0; if (colWidth.Length != m_dtReport.Columns.Count)
return;
else
foreach (DataColumn dc in m_dtReport.Columns)
{
if (colWidth[dc.Ordinal] > 0)
ecol++;
} try
{
// Title
erow = row + 2;
AddTitle(row, col, erow, ecol);
row = row + 3; // Sub-title
AddSubTitle(row, col, row, ecol);
row = row + 1; // Data title
AddDataTitle(row, col, colWidth);
row = row + 1; // Data
AddDataBody(row, col, colWidth); // Style
erow = m_dtReport.Rows.Count + row - 1;
FontName(row, col, erow, ecol);
FontSize(row, col, erow, ecol);
NumberDataFormat(row, erow, colWidth);
SetColWidth(colWidth); // autofit column
m_Sheet.SetColWidthAuto(1, 1, erow, ecol, true); // Page Setup
PageSetup(row - 1); // Save
if (isSave)
Save();
else
m_Sheet.FilePrintPreview();
}
catch (Exception ex)
{
throw new Exception("error from export to excel", ex);
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货