欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C# Winform实现导入和导出Excel文件

时间:2020-11-26 22:15:38|栏目:.NET代码|点击:

本文实例为大家分享了Winform实现导入导出Excel文件的具体代码,供大家参考,具体内容如下

/// <summary>
    /// 导出Excel文件
    /// </summary>
    /// /// <param name="dataSet"></param>
    /// <param name="dataTable">数据集</param>
    /// <param name="isShowExcle">导出后是否打开文件</param>
    /// <returns></returns>
    public static bool DataTableToExcel(string filePath, System.Data.DataTable dataTable, bool isShowExcle)
    {
      //System.Data.DataTable dataTable = dataSet.Tables[0];
      int rowNumber = dataTable.Rows.Count;
      int columnNumber = dataTable.Columns.Count;
      int colIndex = 0;
 
      if (rowNumber == 0)
      {
        return false;
      }
 
      Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
      Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
      Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
      excel.Visible = isShowExcle;
      Microsoft.Office.Interop.Excel.Range range;
 
 
      foreach (DataColumn col in dataTable.Columns)
      {
        colIndex++;
        excel.Cells[1, colIndex] = col.ColumnName;
      }
 
      object[,] objData = new object[rowNumber, columnNumber];
 
      for (int r = 0; r < rowNumber; r++)
      {
        for (int c = 0; c < columnNumber; c++)
        {
          objData[r, c] =dataTable.Rows[r][c];
        }
      }
 
      range = worksheet.get_Range(excel.Cells[2, 1], excel.Cells[rowNumber + 1, columnNumber]);
 
      range.Value2 = objData;
 
      range.NumberFormatLocal = "@";
 
      worksheet.SaveAs(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
 
      //excel.Quit();
      return true;
    }

读取Excel文件数据到DataTable

 /// <summary>
    /// 读取Excel文件数据到DataTable
    /// </summary>
    /// <param name="filePath">Excel文件路径</param>
    private void Import_Excel(string filePath)
    {
      string sqlconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
 
      string sql = @"select * from [Sheet1$]";
 
      try
      {
        using (OleDbConnection conn = new OleDbConnection(sqlconn))
        {
          using (OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn))
          {
            System.Data.DataTable dt = new System.Data.DataTable();
            adapter.Fill(dt);
 
            this.LoadDataGridView(dt);
          }
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show("打开文件出错,错误信息:" + ex.Message.ToString(), "提示");
      }
    }

上一篇:C#创建一个小型Web Server(Socket实现)

栏    目:.NET代码

下一篇:C#实现利用Windows API读写INI文件的方法

本文标题:C# Winform实现导入和导出Excel文件

本文地址:http://www.codeinn.net/misctech/26467.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有