public static void DataTabletoExcel(System.Data.DataTable tmpDataTable,string strFileName)
{
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.Columns.Count;
int rowIndex = 1;
int columnIndex = 0; Excel.Application xlApp = new Excel.ApplicationClass();
xlApp.DefaultFilePath ="";
xlApp.DisplayAlerts = true;
xlApp.SheetsInNewWorkbook = 1;
Excel.Workbook xlBook = xlApp.Workbooks.Add(true); //fieldname を書きます
foreach(DataColumn dc in tmpDataTable.Columns)
{
columnIndex ++;
xlApp.Cells[rowIndex,columnIndex] = dc.ColumnName;
} //データを導出します
for(int i = 0;i<rowNum; i++)
{
rowIndex ++;
columnIndex = 0;
for (int j = 0;j<columnNum; j++)
{
columnIndex ++;
xlApp.Cells[rowIndex,columnIndex] = tmpDataTable.Rows[i][j].ToString();
}
}
xlBook.SaveAs((object)strFileName,objt,objt,objt,objt,objt,Excel.XlSaveAsAccessMode.xlNoChange,objt,objt,objt,objt,objt);
MessageBox.Show ("EXCELL表を作って終わりました!");
xlApp.Quit();
}
参考一下吧!赫赫

解决方案 »

  1.   

    看看http://www.codeproject.com/aspnet/ExportClassLibrary.asp,有詳細的教程,還有Demo和源碼,應該能滿足的要求,不懂再問。
      

  2.   

    DataGrid1.DataSource =(new emp ()).GetYgxi (Session["strbh"].ToString () ,Session["strkh"].ToString () ,Session["strxm"].ToString () ,Session["stryx"].ToString () );
    DataGrid1.DataBind ();
    Response.ContentType ="application/vnd.ms-excel";
    Response.Charset ="";
    EnableViewState=false;
    System.IO .StringWriter tw=new System.IO.StringWriter ();
    System.Web .UI .HtmlTextWriter hw=new HtmlTextWriter (tw);
    DataGrid1.RenderControl (hw);
    Response.Write (tw.ToString ());
    Response.End() ;
      

  3.   

    xlBook.SaveAs((object)strFileName,objt,objt,objt,objt,objt,Excel.XlSaveAsAccessMode.xlNoChange,objt,objt,objt,objt,objt);
    这个地方出了些问题,不知道该怎么解决了
      

  4.   

    http://blog.csdn.net/blackhero/archive/2006/08/25/1116399.aspx
      

  5.   


         private void SaveToExcel()
          {
              SaveFileDialog SFDia = new SaveFileDialog();
              SFDia.Filter = "Exel files (*.xls)|*.xls";
              SFDia.FilterIndex = 2;
              SFDia.RestoreDirectory = true;
              SFDia.OverwritePrompt = true;
              SFDia.FileName = "";
              SFDia.ShowDialog();
              string FileName = SFDia.FileName;
              if (FileName == "")
                  return;
              FileStream objFileStream;
              StreamWriter objStreamWriter;
              string strLine = "";
              objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
              objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
              for (int i = 0; i < FTableResult.Columns.Count; i++)
              {
                  strLine = strLine + FTableResult.Columns[i].ColumnName.ToString() + Convert.ToChar(9);
              }
              objStreamWriter.WriteLine(strLine);
              strLine = "";
              for (int i = 0; i < FTableResult.Rows.Count; i++)
              {
                  for (int j = 0; j < FTableResult.Columns.Count; j++)
                  {
                      strLine = strLine + FTableResult.Rows[i][j].ToString() + Convert.ToChar(9);
                  }
                  objStreamWriter.WriteLine(strLine);
                  strLine = "";
              }
              objStreamWriter.Close();
              objFileStream.Close();      }