public int ExportExcel(string fileName)
{
///启动Excel
Cursor.Current = Cursors.WaitCursor; Excel.Application app = null ;
try
{
app = new Excel.Application();
}
catch
{
} if (app == null) 
{
MessageBox.Show("启动Excel失败!","失败",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
Cursor.Current = Cursors.Default;
return 1;
} Workbooks workbooks = app.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
///添加worksheet
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet) sheets.get_Item(1); if (worksheet == null) 
{
MessageBox.Show("启动Excel失败!","失败",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
Cursor.Current = Cursors.Default;
return 2;
} if (DataGridSource.TableStyles.Count > 0)
{
//写标题行
int row = 1;
for (int ii = 0 ; ii<DataGridSource.TableStyles[0].GridColumnStyles.Count ;ii ++)
{
if (DataGridSource.TableStyles[0].GridColumnStyles[ii].Width > 0)
{
worksheet.Cells[row,ii+1] = DataGridSource.TableStyles[0].GridColumnStyles[ii].HeaderText ;
}
//DrawBoxString(e ,font,System.Drawing.Brushes.LightGray ,DataGridSource.TableStyles[0].GridColumnStyles[ii].HeaderText ,1 ,1 ,newWidth[ii] );
} //写表格内容
DataView GridDV = null;
if (DataGridSource.DataSource.GetType().Name == "DataTable")
{
GridDV = ((System.Data.DataTable) DataGridSource.DataSource).DefaultView ;
}
if (DataGridSource.DataSource.GetType().Name == "DataView")
{
GridDV = (DataView)DataGridSource.DataSource;
}

for (int ii = 0 ;ii < GridDV.Table.Rows.Count ;ii++)
{
row ++ ;
for (int jj = 0 ; jj<DataGridSource.TableStyles[0].GridColumnStyles.Count ;jj ++)
{
if (DataGridSource.TableStyles[0].GridColumnStyles[jj].Width > 0)//需要判断DBNull
{
int index=0;
string substr="";
substr = " 00:00:00";
string content = GridDV.Table.Rows[ii][DataGridSource.TableStyles[0].GridColumnStyles[jj].MappingName].ToString();
index=content.IndexOf(substr);
if(index>0)
{
worksheet.Cells[row,jj+1] = content.Substring(0,index);
}
else
{
substr = " 0:00";
index=content.IndexOf(substr);
if(index>0)
{
worksheet.Cells[row,jj+1] = content.Substring(0,index);
}
else
worksheet.Cells[row,jj+1] = GridDV.Table.Rows[ii][DataGridSource.TableStyles[0].GridColumnStyles[jj].MappingName].ToString() ;
}
}
}
}
}
else
{
System.Data.DataTable dt = (System.Data.DataTable)(DataGridSource.DataSource) ;
//写标题行
int row = 1;
for (int ii = 0 ; ii<dt.Columns.Count ;ii ++)
{
worksheet.Cells[row,ii+1] = dt.Columns[ii].Caption ;
} //写表格内容
DataView GridDV = null;
if (DataGridSource.DataSource.GetType().Name == "DataTable")
{
GridDV = ((System.Data.DataTable) DataGridSource.DataSource).DefaultView ;
}
if (DataGridSource.DataSource.GetType().Name == "DataView")
{
GridDV = (DataView)DataGridSource.DataSource;
}

for (int ii = 0 ;ii < GridDV.Table.Rows.Count ;ii++)
{
row ++ ;
for (int jj = 0 ; jj<dt.Columns.Count ;jj ++)
{
int index=0;
string substr="";
substr = " 00:00:00";
string content = GridDV.Table.Rows[ii][DataGridSource.TableStyles[0].GridColumnStyles[jj].MappingName].ToString();
index=content.IndexOf(substr);
if(index>0)
{
worksheet.Cells[row,jj+1] = content.Substring(0,index);
}
else
{
substr = " 0:00";
index=content.IndexOf(substr);
if(index>0)
{
worksheet.Cells[row,jj+1] = content.Substring(0,index);
}
else
worksheet.Cells[row,jj+1] = GridDV.Table.Rows[ii][DataGridSource.TableStyles[0].GridColumnStyles[jj].MappingName].ToString() ;
}
}
}
} ///保存数据
worksheet.SaveAs(fileName ,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value ); ///结束进程
try
{
System.Threading.Thread.Sleep(100);
}
catch(COMException ex) 
{
MessageBox.Show(ex.ToString(),"失败",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
Cursor.Current = Cursors.Default;
return 3;
} try 
{
// If user interacted with Excel it will not close when the app object is destroyed, so we close it explicitely
workbook.Saved = true;
app.UserControl = false;
app.Quit();
// 关闭进程,保留用户自启动的进程
Process [] localByName = System.Diagnostics.Process.GetProcessesByName("Excel");
foreach(Process excelapp in localByName)
{
if((int)excelapp.MainWindowHandle==0)
{
excelapp.Kill();
excelapp.Close();
}
}

catch (COMException ex) 
{
MessageBox.Show(ex.ToString(),"失败",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
Cursor.Current = Cursors.Default;
return 4;
} Cursor.Current = Cursors.Default;
return 0;
}

解决方案 »

  1.   

    解释一下这些东西都是些什么?
    worksheet.SaveAs(fileName ,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value );还有你用的导出方法是基于office的那个版本??
      

  2.   

    保存工作单元至文件的方法,Missing.Value是参数,表示丢失的,未知的值,好像是2000:)
      

  3.   

    Missing.Value
    是在哪个命名空间下引用才好用的??
      

  4.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=BF0A54F9-C7C7-4200-BD9A-802AC1F5DE50