首页 > Windows开发 > 详细

C# 将内存中的datatable数据导出为Excel(方法二,创建Excel对象导出)【转载】

时间:2015-02-06 09:20:29      阅读:317      评论:0      收藏:0      [点我收藏+]

public  void DataTabletoExcel(System.Data.DataTable[] tmpDataTable,string date1,string date2)

         {

             string saveFileName = "";

                         SaveFileDialog saveDialog = new SaveFileDialog();

             saveDialog.DefaultExt = "xls";

             saveDialog.Filter = "Excel 文件|*.xls";

                        saveDialog.ShowDialog();

             saveFileName = saveDialog.FileName;

             if (saveFileName.IndexOf(":") < 0)

                   return; //被点了取消

              for (int k = 0; k < tmpDataTable.Length; k++)

             {

                 //if (k % 50 == 0)

                 //{

                     Process[] processes = Process.GetProcesses();

                     foreach (Process process in processes)

                     {

                         if (process.ProcessName == "EXCEL")

                         {

                             if (string.IsNullOrEmpty(process.MainWindowTitle))

                             {

                                 process.Kill();

                             }

                                                     }

                     }

                   //}

                 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

                 try

                 { 

                    #region

                     if (tmpDataTable[k] == null)

                        return;

                     string name=string.Empty;

                     if (grid_Result1.GetGridView.Columns["STR_1"].Visible == false && grid_Result1.GetGridView.Columns["STR_8"].Visible == false)

                         name = tmpDataTable[k].Rows[0][0].ToString();

                     if (grid_Result1.GetGridView.Columns["STR_1"].Visible == false && grid_Result1.GetGridView.Columns["STR_8"].Visible == true)

                         name = tmpDataTable[k].Rows[0][1].ToString();

                     if (grid_Result1.GetGridView.Columns["STR_1"].Visible == true && grid_Result1.GetGridView.Columns["STR_8"].Visible == true)

                         name = tmpDataTable[k].Rows[0][2].ToString();

                     if (grid_Result1.GetGridView.Columns["STR_1"].Visible == true && grid_Result1.GetGridView.Columns["STR_8"].Visible == false)

                         name = tmpDataTable[k].Rows[0][1].ToString();

                     if (name.IndexOf("/") > -1)

                     {

                         name = name.Replace("/", " ");

                     }

                     name = name + date1 + "~" + date2;

                     saveFileName = saveFileName.Substring(0, saveFileName.LastIndexOf("\\")) + "\\" + name + ".xls";

                     int rowNum = tmpDataTable[k].Rows.Count;

                    int columnNum = tmpDataTable[k].Columns.Count;

                    int rowIndex = 1;

                    int columnIndex = 0;

                     xlApp.DefaultFilePath = "";

                     xlApp.DisplayAlerts = true;

                     xlApp.SheetsInNewWorkbook = 1;

                     Workbook xlBook = xlApp.Workbooks.Add(true);

                     //将DataTable的列名导入Excel表第一行

                    foreach (DataColumn dc in tmpDataTable[k].Columns)

                     {

                         columnIndex++;

                         xlApp.Cells[rowIndex, columnIndex] = dc.ColumnName;

                     }

                     //将DataTable中的数据导入Excel中

                    for (int i = 0; i < rowNum; i++)

                     {

                         rowIndex++;

                         columnIndex = 0;

                         for (int j = 0; j < columnNum; j++)

                         {

                             columnIndex++;

                             xlApp.Cells[rowIndex, columnIndex] = tmpDataTable[k].Rows[i][j].ToString(); 

                        }

                    }

                    if (saveFileName.IndexOf("/") > -1)

                     {

                         saveFileName = saveFileName.Replace("/", "-");

                     }

                     System.IO.File.Delete(saveFileName);

                     xlBook.SaveCopyAs(saveFileName);

                     Process[] processes1 = Process.GetProcesses();

                     foreach (Process process in processes1)

                     {

                         if (process.ProcessName == "EXCEL")

                         {

                             if (string.IsNullOrEmpty(process.MainWindowTitle))

                             {

                                 process.Kill();

                             }

                        } 

                    }

                     System.Threading.Thread.Sleep(1000);

                     #endregion

                 } 

                catch (Exception ex)

                 { 

                    XtraMessageBox.Show("导出Excel出错,原因:" + ex.Message);

                     return;

                 }

        }

 

C# 将内存中的datatable数据导出为Excel(方法二,创建Excel对象导出)【转载】

原文:http://www.cnblogs.com/happylyyer/p/4276351.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!