C#datagridview导出数据到excel

来源:互联网 发布:淘宝服装促销活动方案 编辑:程序博客网 时间:2024/05/16 14:08
 
  1. 添加dll引用
  2. 右击选择你所在的项目的“引用”,选择“添加引用”。
  3. 弹出“添加引用”对话框。
  4. 选择“COM”选项卡。
  5. 选择“Microsoft Excel 11.0 Object Library”
  6. 单击“确定”按钮。
  7. 代码
  8. public static bool ExportForDataGridview(DataGridView gridView, string fileName, bool isShowExcle)
  9.         {
  10.             //建立Excel对象
  11.             Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
  12.             try
  13.             {
  14.                 if (app == null)
  15.                 {
  16.                     return false;
  17.                 }
  18.                 
  19.                 app.Visible = isShowExcle;
  20.                 Workbooks workbooks = app.Workbooks;
  21.                 _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
  22.                 Sheets sheets = workbook.Worksheets;
  23.                 _Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
  24.                 if (worksheet == null)
  25.                 {
  26.                     return false;
  27.                 }
  28.                 string sLen = "";
  29.                 //取得最后一列列名
  30.                 char H = (char)(64 + gridView.ColumnCount / 26);
  31.                 char L = (char)(64 + gridView.ColumnCount % 26);
  32.                 if (gridView.ColumnCount < 26)
  33.                 {
  34.                     sLen = L.ToString();
  35.                 }
  36.                 else
  37.                 {
  38.                     sLen = H.ToString() + L.ToString();
  39.                 }
  40.                 //标题
  41.                 string sTmp = sLen + "1";
  42.                 Range ranCaption = worksheet.get_Range(sTmp, "A1");
  43.                 string[] asCaption = new string[gridView.ColumnCount];
  44.                 for (int i = 0; i < gridView.ColumnCount; i++)
  45.                 {
  46.                     asCaption[i] = gridView.Columns[i].HeaderText;
  47.                 }
  48.                 ranCaption.Value2 = asCaption;
  49.                 //数据
  50.                 object[] obj = new object[gridView.Columns.Count];
  51.                 for (int r = 0; r < gridView.RowCount - 1; r++)
  52.                 {
  53.                     for (int l = 0; l < gridView.Columns.Count; l++)
  54.                     {
  55.                         if (gridView[l, r].ValueType == typeof(DateTime))
  56.                         {
  57.                             obj[l] = gridView[l, r].Value.ToString();
  58.                         }
  59.                         else
  60.                         {
  61.                             obj[l] = gridView[l, r].Value;
  62.                         }
  63.                     }
  64.                     string cell1 = sLen + ((int)(r + 2)).ToString();
  65.                     string cell2 = "A" + ((int)(r + 2)).ToString();
  66.                     Range ran = worksheet.get_Range(cell1, cell2);
  67.                     ran.Value2 = obj;
  68.                 }
  69.                 //保存
  70.                 workbook.SaveCopyAs(fileName);
  71.                 workbook.Saved = true;
  72.             }
  73.             finally
  74.             {
  75.                 //关闭
  76.                 app.UserControl = false;
  77.                 app.Quit();
  78.             }
  79.             return true;
  80.         }
  81. 方法2
  82. 用流保存成xls文件. 这种方法比较好,不用引用Excel组件.   下面是具体例子,可以参考
  83. using System.IO;
  84.         
  85.         /// <summary>
  86.         /// 另存新档按钮
  87.         /// </summary>
  88.         private void SaveAs() //另存新档按钮   导出成Excel
  89.         {
  90.             SaveFileDialog saveFileDialog = new SaveFileDialog();
  91.             saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
  92.             saveFileDialog.FilterIndex = 0;
  93.             saveFileDialog.RestoreDirectory = true;
  94.             saveFileDialog.CreatePrompt = true;
  95.             saveFileDialog.Title = "Export Excel File To"
  96.             saveFileDialog.ShowDialog();
  97.             Stream myStream;
  98.             myStream = saveFileDialog.OpenFile();
  99.             //StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
  100.             StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
  101.             string str = "";
  102.             try
  103.             {
  104.                 //写标题
  105.                 for (int i = 0; i < dgvAgeWeekSex.ColumnCount; i++)
  106.                 {
  107.                     if (i > 0)
  108.                     {
  109.                         str += "/t";
  110.                     }
  111.                     str += dgvAgeWeekSex.Columns[i].HeaderText;
  112.                 }
  113.                 sw.WriteLine(str);
  114.                 //写内容
  115.                 for (int j = 0; j < dgvAgeWeekSex.Rows.Count; j++)
  116.                 {
  117.                     string tempStr = "";
  118.                     for (int k = 0; k < dgvAgeWeekSex.Columns.Count; k++)
  119.                     {
  120.                         if (k > 0)
  121.                         {
  122.                             tempStr += "/t";
  123.                         }
  124.                         tempStr += dgvAgeWeekSex.Rows[j].Cells[k].Value.ToString();
  125.                     }
  126.                     
  127.                     sw.WriteLine(tempStr);                    
  128.                 }
  129.                 sw.Close();
  130.                 myStream.Close();
  131.             }
  132.             catch (Exception e)
  133.             {
  134.                 MessageBox.Show(e.ToString());
  135.             }
  136.             finally
  137.             {
  138.                 sw.Close();
  139.                 myStream.Close();
  140.             }           
  141.       }
  142. 第一总方法
  143. 补充一下
  144. 需要添加dll引用
  145. 右击选择你所在的项目的“引用”,选择“添加引用”。
  146. 弹出“添加引用”对话框。
  147. 选择“COM”选项卡。
  148. 选择“Microsoft Excel 11.0 Object Library”
  149. 单击“确定”按钮。
  150. 还有一个Microsoft Office Object的组件,要一并引入才可以,这个是基类
  151. 需用根据你引用Excel,可以用下句引用Office2003/2007均可 
  152. 还需要using一下
  153. using Excel = Microsoft.Office.Interop.Excel;
  154. 因为Excel版本不同,所以打出来的dll的namespace也不同
  155. 运行通过
原创粉丝点击