• 用C#快速往Excel写数据


    用C#快速往Excel写数据

      本示例是用于将ListView中的内容倒入到Excel,与常用的逐单元格写不同的是,本例子采用数据写入到range的方法。该方法效率

    明显较高。

       Excel.Application app = new Excel.ApplicationClass();
      if( app == null)
      {
      MessageBox.Show("Excel无法启动");
      return;
      }
      app.Visible = true;
      Excel.Workbooks wbs = app.Workbooks;
      Excel.Workbook wb = wbs.Add(Missing.Value);
      Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
      Excel.Range r = ws.get_Range("A1","H1");
      object [] objHeader = {"标题1","标题2","标题3",
      "标题4","标题5","标题6",
      "标题7","标题8"};
      r.Value = objHeader;
      if (lv.Items.Count >0)
      {
      r = ws.get_Range("A2",Missing.Value); 
      object [,] objData = new Object[this.lv.Items.Count,8];
      foreach(ListViewItem lvi in lv.Items)
      {
      objData[lvi.Index,0] = lvi.Text;
      objData[lvi.Index,1] = lvi.SubItems[1].Text;
      objData[lvi.Index,2] = lvi.SubItems[2].Text;
      objData[lvi.Index,3] = lvi.SubItems[3].Text;
      objData[lvi.Index,4] = lvi.SubItems[4].Text;
      objData[lvi.Index,5] = lvi.SubItems[5].Text;
      objData[lvi.Index,6] = lvi.SubItems[6].Text;
      objData[lvi.Index,7] = lvi.SubItems[7].Text;
      }
      r = r.get_Resize(lv.Items.Count,8);
      r.Value = objData;
      r.EntireColumn.AutoFit();
      }
      app = null;

  • 相关阅读:
    IEEE 754 浮点数的表示方法
    .NET Core 3.0及以上的EFCore连接MySql
    一些常见错误/技巧/结论总结
    2-sat学习笔记
    动态DP学习笔记
    动态规划优化算法——wqs二分 and 折线优化
    扩展莫队小总结(二) (回滚莫队/二次离线莫队)
    CF1504X Codeforces Round #712
    CF1500D Tiles for Bathroom (递推+大讨论)
    CF1486X Codeforces Round #703
  • 原文地址:https://www.cnblogs.com/soundcode/p/1910614.html
Copyright © 2020-2023  润新知