程序的代码如下:
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 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);
// 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);
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