C# 将内存中的datatable数据导出为Excel(方法二,创建Excel对象导出)
上次寫了一個用文件流方式將Datatable導出Excel的方法,這個方法有局限性,比如沒法對Excel進行一些增加列顏色等簡單的操作,現在,給大家介紹另外一種方法,用微軟的Excel類。既然要用到類,那必須是你的機子要裝上Excel才行呢。
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;
???????????????? }
??????? }
?
?
?
轉載于:https://www.cnblogs.com/dogxuefeng/archive/2012/04/09/2439155.html
總結
以上是生活随笔為你收集整理的C# 将内存中的datatable数据导出为Excel(方法二,创建Excel对象导出)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一周以来的工作总结--oracle分区的
- 下一篇: c# winform TreeView与