把datatable导出到指定的excel中

首先要在项目中引用Microsoft.Office.Interop.Excel.dll

然后编写代码如下

 FileInfo fin=new FileInfo(pathselect.ToString() + @"\第一财经(股指期货融资融券).xls");
                     if(fin.Exists)
                     fin.Delete();
                   
                     Microsoft.Office.Interop.Excel._Application xlapp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                     Microsoft.Office.Interop.Excel.Workbook xlbook = xlapp.Workbooks.Add(true);
                     ((Microsoft.Office.Interop.Excel.Worksheet)xlbook.Sheets.get_Item(1)).Copy(Type.Missing, ((Microsoft.Office.Interop.Excel.Worksheet)xlbook.Sheets.get_Item(1)));
                     Microsoft.Office.Interop.Excel.Worksheet xlsheet = (Microsoft.Office.Interop.Excel.Worksheet)xlbook.Worksheets[2];
                     xlsheet.Name = "融资融券交易明细";
                     Microsoft.Office.Interop.Excel.Range range = xlsheet.get_Range(xlsheet.Cells[1, 1], xlsheet.Cells[1, table1.Columns.Count]);

                     range.MergeCells = false;//是否合并单元格
                     range.Font.ColorIndex = 3;//选择的颜色,此时为红色
                     range.Font.Size =9;//文字大小
                     range.Font.Bold = true;
                     range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//水平位置

                     range = xlsheet.get_Range(xlsheet.Cells[2, 2], xlsheet.Cells[table.Rows.Count + 2, 2]);
                     range.NumberFormatLocal = "@";//设置第二列为文本格式
                     range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                     range = xlsheet.get_Range(xlsheet.Cells[2, 3], xlsheet.Cells[table.Rows.Count + 2, 3]);
                     range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;

                     range = xlsheet.get_Range(xlsheet.Cells[2, 1], xlsheet.Cells[table.Rows.Count + 2, table.Columns.Count]);
                     range.MergeCells = false;
                     range.Font.Size = 9;
                     int colIndex = 0;//起始列号
                     int RowIndex = 1;//起始行号
                     //开始写入每列的标题
                     foreach (DataColumn dc in table.Columns)
                     {
                         colIndex++;
                         xlsheet.Cells[RowIndex, colIndex] = dc.Caption;
                     }
                     //开始写入内容
                     int RowCount = table.Rows.Count;//行数
                     for (int i = 0; i < RowCount; i++)
                     {
                         RowIndex++;
                         int ColCount = table.Columns.Count;//列数
                         for (colIndex = 1; colIndex <= ColCount; colIndex++)
                         {
                            
                             xlsheet.Cells[RowIndex, colIndex] = table.Rows[i][colIndex - 1].ToString();//dg[i, colIndex - 1];
                             xlsheet.Cells.ColumnWidth = table.Rows[i][colIndex - 1].ToString().Length;
                         }
                     }
                     xlbook.Saved = true;
                     Microsoft.Office.Interop.Excel.Worksheet oSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlbook.Worksheets[1];
                     oSheet.Name = "股指期货交易行情";

                     range = oSheet.get_Range(oSheet.Cells[1, 1], oSheet.Cells[1, table1.Columns.Count]);
                     range.MergeCells = false;
                     range.Font.Size = 10;
                     range.Font.Bold = true;
                     range.Font.ColorIndex = 3;//红色

                     range = oSheet.get_Range(oSheet.Cells[2, 1], oSheet.Cells[table.Rows.Count + 2, table1.Columns.Count]);
                     range.MergeCells = false;
                     range.Font.Size = 12;
                     //range.Font.Bold = true;
                     oSheet.Cells[1, 1] = "交易日期";
                     oSheet.Cells[1, 2] = "市场代码";
                     oSheet.Cells[1, 3] = "内部代码";
                     oSheet.Cells[1, 4] = "合约代码";
                     oSheet.Cells[1, 5] = "合约名称";
                     oSheet.Cells[1, 6] = "昨收盘(元)";
                     oSheet.Cells[1, 7] = "开盘价(元)";
                     oSheet.Cells[1, 8] = "最高价(元)";
                     oSheet.Cells[1, 9] = "最低价(元)";
                     oSheet.Cells[1, 10] = "收盘价(元)";
                     oSheet.Cells[1, 11] = "结算价(元)";
                     oSheet.Cells[1, 12] = "涨跌(元)";
                     oSheet.Cells[1, 13] = "涨跌幅(%)";
                     oSheet.Cells[1, 14] = "成交量(手)";
                     oSheet.Cells[1, 15] = "成交金额(万元)";
                     oSheet.Cells[1, 16] = "持仓量(手)";
                     oSheet.Cells[1, 17] = "涨停价(元)";
                     oSheet.Cells[1, 18] = "跌停价(元)";
                     for (int c = 0; c < table1.Columns.Count; c++)
                     {
                         //oSheet.Cells[1, c + 1] = table1.Columns[c];
                         for (int r = 1; r <= table1.Rows.Count; r++)
                         {
                             oSheet.Cells[r + 1, c + 1] = table1.Rows[r - 1][c].ToString();
                         }
                     }
                     xlbook.Saved = true;
                     xlbook.SaveCopyAs(pathselect.ToString() + @"\第一财经(股指期货融资融券).xls");
                     xlapp.Quit();
                     GC.Collect();

原文地址:https://www.cnblogs.com/lijinchang/p/2375225.html