用C#将数据写入到Excel指定的单元格中

  1. 在使用用本文的方法前,需要先安装Office 的Excel程序,然后,引用 Excel 的对象包装:
  1. using Microsoft.Office.Interop.Excel;

 

不同的Office版本可能引用的名称不同。

使用下面的代码,完成数据写入操作:

  1.  private void writeData(string excelFileName,DataSet dsResult,)
  2.         {
  3.             _Application xlApp = null;
  4.             _Workbook xlWorkbook = null;
  5.             _Worksheet xlWorksheet = null;
  6.             
  7.             
  8.             System.Reflection.Missing missing = System.Reflection.Missing.Value;
  9.             string saveAsPath = "";
  10.             try
  11.             {
  12.                 xlApp = new ApplicationClass();
  13.                 xlApp.Visible =true ;
  14.                 xlWorkbook=xlApp.Workbooks.Open(excelFileName, missing, missing, missing, missing, missing, missing, missing, 
  15.                     missing, missing, missing, missing, missing, missing, missing);
  16.                 xlWorksheet = xlWorkbook.Worksheets[1] as _Worksheet;
  17.                 //xlWorksheet.Name = "NewWorksheet";
  18.                 //xlWorksheet.Cells[1, "A"] = "Topic: ";
  19.                 //xlWorksheet.Cells[1, "B"] = ".Net Interop Excel Demo";
  20.                 for (int x = 4; x < 92; x++)
  21.                 {
  22.                     object rang = xlWorksheet.Cells[x, "D"];
  23.                     if (rang != null)
  24.                     {
  25.                         object objText = ((Range)rang).Value2;
  26.                         string strCellText = objText==null?"":objText.ToString();
  27.                         if (strCellText == "")
  28.                         {
  29.                             rang = xlWorksheet.Cells[x, "C"];
  30.                             strCellText = "";
  31.                             if (rang != null)
  32.                             {
  33.                                 objText = ((Range)rang).Value2;
  34.                                 strCellText = objText == null ? "" : objText.ToString();
  35.                             }
  36.                             
  37.                         }
  38.                         //xlWorksheet.Cells[x, "E"] = strCellText;
  39.                         DataRow[] rows = dsResult.Tables[0].Select("Name='" + strCellText + "'");
  40.                        if (rows.Length > 0)
  41.                        {
  42.                            //防止有空值
  43.                            if(rows[0]["Count"]!=DBNull.Value )
  44.                             xlWorksheet.Cells[x, "H"] = rows[0]["Count"].ToString();
  45.                                                  }
  46.                     }
  47.                     
  48.                 }
  49.                 
  50.                 
  51.                 
  52.                 
  53.                 xlWorkbook.Save();
  54.                 //saveAsPath = System.Windows.Forms.Application.StartupPath + "//" + xlWorkbook.Name;
  55.                 //xlWorkbook.SaveAs(saveAsPath, missing, missing, missing, missing,
  56.                 //    missing, XlSaveAsAccessMode.xlShared, missing, missing, missing,
  57.                 //    missing, missing);
  58.                 //xlApp.Quit();
  59.             }
  60.             catch (Exception ex)
  61.             {
  62.                 MessageBox.Show(ex.Message);
  63.             }
  64.             finally
  65.             {
  66.                 //System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
  67.                 //xlApp = null;
  68.                 //GC.Collect();
  69.             }
  70.         }

本例没有直接关闭文件,如果你要另存为一个文件,把注释取消即可。

注意获取单元格的方法:

 

 

  1.  rang = xlWorksheet.Cells[x, "C"];
  2.                             strCellText = "";
  3.                             if (rang != null)
  4.                             {
  5.                                 objText = ((Range)rang).Value2;
  6.                                 strCellText = objText == null ? "" : objText.ToString();
  7.                             }

 

 

 

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值