用EXCEL Application产生excel的问题

zhucheng 2003-08-20 08:54:29
我在asp.net里面,用EXCEL Application生成了一个excel文件,但是生成的excel文件用excel xp打开以后,就会报告程序出现错误,并且退出excel。我在多台机器上试了,都是这个现象,请问是什么原因。我的机器配置为:windows2000 pro,vs.net,office xp.
...全文
53 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
panyee 2003-08-20
  • 打赏
  • 举报
回复
m_oExcelApp.Save(filename);//这种方法试过没有?
zhucheng 2003-08-20
  • 打赏
  • 举报
回复
程序的代码如下:
public class ProcessReport
{
// Private data
private Excel.ApplicationClass m_oExcelApp;
private Excel.Workbooks m_oBooks;
private Excel._Workbook m_oBook;
private Excel._Worksheet m_oSheet;
private object m_oMissing;
private string m_sReportTemplate;
private int m_nReportIndex;
private string m_sDataTemplate;
private string m_sExportFormat;
private string m_sOutputCache;

// Set properties
public string ReportTemplate { get { return m_sReportTemplate; } set { m_sReportTemplate = value;} }
public string DataTemplate { get { return m_sDataTemplate; } set { m_sDataTemplate = value;} }
public int ReportIndex { get { return m_nReportIndex; } set { m_nReportIndex = value;} }
public string ExportFormat { get { return m_sExportFormat; } set { m_sExportFormat = value;} }
public string OutputCache { get { return m_sOutputCache; } set { m_sOutputCache = value;} }

public ProcessReport()
{
m_oExcelApp = null;
m_oBooks = null;
m_oBook = null;
m_oSheet = null;
m_oMissing = System.Reflection.Missing.Value;
ReportIndex = 1;
}

public bool GetTestReport(out string sReportFile, out string sMessage)
{
sReportFile = null;
sMessage = "";
string sDeleteFile = null;
try
{
// Get temporary file name
sReportFile = Path.GetTempFileName();
File.Copy(m_sReportTemplate,sReportFile,true);
m_sReportTemplate = sReportFile;

// Get valid report tempate
OpenReportTempalte();

// Get employee data
DataSet oRptData = new DataSet();
oRptData.ReadXml(m_sDataTemplate);

// Write employee data to spreadsheet (Excel)
int nRow = 2;
foreach(DataRow oRow in oRptData.Tables["Group"].Rows)
{
m_oSheet.Cells[nRow,1] = oRow["Name"];
m_oSheet.Cells[nRow,2] = oRow["TestA"];
m_oSheet.Cells[nRow,3] = oRow["TestB"];
m_oSheet.Cells[nRow,4] = oRow["TestC"];

nRow++;
}

// Save data to temorary (sReportFile)
if(m_sExportFormat.ToUpper().CompareTo("HTML") == 0)
{
// get file name with out path
FileInfo oFInfo = new FileInfo(sReportFile);

// set delete file to orginal file
sDeleteFile = sReportFile;
sReportFile = oFInfo.Name.Replace(".","") + "_Export.htm";
string sReportFileLocal = m_sOutputCache + "\\" + sReportFile;

// Delete if html file already exists
oFInfo = new FileInfo(sReportFileLocal);
if(oFInfo.Exists)
File.Delete(sReportFileLocal);

// Export to HTML format
//m_oBook.SaveAs(sReportFileLocal,Excel.XlFileFormat.xlHtml,m_oMissing,m_oMissing,m_oMissing,m_oMissing,
// Excel.XlSaveAsAccessMode.xlNoChange,m_oMissing,m_oMissing,m_oMissing,m_oMissing);

}
else
m_oBook.Save();

// Done
return true;
}
catch(Exception exp)
{
sMessage = exp.Message;
return false;
}
finally
{
CloseReportTemplate();
if(sDeleteFile != null)
File.Delete(sDeleteFile);
}
}

public void OpenReportTempalte()
{
if(m_oExcelApp != null)
CloseReportTemplate();

// Create an instance of Microsoft Excel, make it visible,
// and open Book1.xls.
m_oExcelApp = new Excel.ApplicationClass();
m_oExcelApp.Visible = false;
m_oBooks = m_oExcelApp.Workbooks;

// IMPORTANT: IF YOU ARE USING EXCEL Version >= 10.0 Use function
// prototype in "EXCEL VERSION 10.0" section.
// For Excel Version 9.0 use default "EXCEL VERSION = 9.0".
// This application is not tested with versions lower than Excel 9.0
// Or greater than 10.0

// EXCEL VERSION 10.0
m_oBook = m_oBooks.Open(m_sReportTemplate, m_oMissing, m_oMissing,
m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing,
m_oMissing, m_oMissing, m_oMissing, m_oMissing,m_oMissing,m_oMissing);
// END EXCEL VERSION 10.0

// EXCEL VERSION 9.0
//m_oBook = m_oBooks.Open(m_sReportTemplate, m_oMissing, m_oMissing,
// m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing, m_oMissing,
// m_oMissing, m_oMissing, m_oMissing, m_oMissing);
// END EXCEL VERSION 9.0

m_oSheet = (Excel._Worksheet)m_oBook.Worksheets[m_nReportIndex];
}

private void CloseReportTemplate()
{
if(m_oBook != null)
m_oBook.Close(true, m_sReportTemplate, m_oMissing);

// Quit Excel and clean up.
if(m_oSheet != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (m_oSheet);
m_oSheet = null;
if(m_oBook != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (m_oBook);
m_oBook = null;
if(m_oBooks != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject (m_oBooks);
m_oBooks = null;
if(m_oExcelApp != null)
{
m_oExcelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (m_oExcelApp);
m_oExcelApp = null;
}
}
}
}
panyee 2003-08-20
  • 打赏
  • 举报
回复
生成的文件有错
看看你的生成代码?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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