【问题标题】:How to fill a Listview with an Excel Sheet? (C#, Windows Forms)如何用 Excel 工作表填充 Listview? (C#,Windows 窗体)
【发布时间】:2020-04-22 11:14:40
【问题描述】:

问题在标题中。 我在互联网上没有找到答案。问题:我不能使用 OleDbConnection,因为我必须将文件发送给其他人。

我厌倦了寻找可能不存在的答案,如果有人可以帮助我,我将不胜感激。

这是我的代码,我卡在填充中:

public partial class Form1 : Form
{
    string fname;

    public Form1()
    {
        InitializeComponent();

        fname = "";
        OpenFileDialog fdlg = new OpenFileDialog();
        fdlg.Title = "Excel File Dialog";
        fdlg.InitialDirectory = @"c:\";
        fdlg.Filter = "Excel .csv (*.csv)|*.csv";
        fdlg.FilterIndex = 1;
        fdlg.RestoreDirectory = true;
        if (fdlg.ShowDialog() == DialogResult.OK)
        {
            fname = fdlg.FileName;
        }

        Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
        xla.Visible = true;
        Microsoft.Office.Interop.Excel.Workbook wb = xla.Workbooks.Open(fname);
        Microsoft.Office.Interop.Excel.Worksheet ws = wb.Worksheets[1];
        Microsoft.Office.Interop.Excel.Range xlRange = ws.UsedRange;

        int rowCount = xlRange.Rows.Count;
        int colCount = xlRange.Columns.Count;

        for (int i = 0; i <= rowCount; i++)
        {
            for (int j = 1; j <= colCount; j++)
            {
                listView.Columns.Add(xlRange.Columns.Text);     //This line is not working
            }
        }

        xla.Quit();
        xla = null;
    }

【问题讨论】:

    标签: c# excel winforms listview


    【解决方案1】:

    我已经使用以下方法将数据读入一个大对象

    object[,] values = (object[,])xlRange.Value2;

    并通过以下方式访问它: string nextVal = values[iRow, iCol] as string;

    【讨论】:

      【解决方案2】:

      如果您使用 csv,它只是一个文本文件,每行一行,列由一些分隔符(例如分号)分隔。

      所以你可以阅读每一行并用分隔符分割它。然后,您可以遍历每一行(= excel 行)和拆分数组内部(= excel 列/单元格)。

      如果它是第一行,您应该为列表视图中的每个行条目添加一列。然后为每个迭代条目添加一个项目到当前列。您可以通过内部迭代中使用的索引访问该列。

      给出一个完整的工作解决方案:

      public Form1()
      {
          InitializeComponent();
      
          OpenFileDialog fdlg = new OpenFileDialog();
          fdlg.Title = "Excel File Dialog";
          fdlg.InitialDirectory = @"c:\";
          fdlg.Filter = "Excel .csv (*.csv)|*.csv";
          fdlg.FilterIndex = 1;
          fdlg.RestoreDirectory = true;
      
          if (fdlg.ShowDialog() == DialogResult.OK)
          {
              var lines = File.ReadAllLines(fdlg.FileName);
              int rowCount = lines.Length;
      
              for (int i = 0; i < rowCount; i++)
              {
                  // split the row into cell values
                  var cells = lines[i].Split(';');
      
                  if (i == 0) // first row -> create column headers
                  {
                      for (int j = 0; j < cells.Length; j++)
                      {
                          listView.Columns.Add(cells[j]);
                      }
                  }
                  else // not first row -> add values
                  {
                      // list view is organized as rows -> create a new row and add first item
                      var newListViewRow = listView.Items.Add(cells[0]);
      
                      // then add the rest of the values as subitems to the listview row
                      for (int j = 1; j < cells.Length; j++)
                      {
                          newListViewRow.SubItems.Add(cells[j]);
                      }
                  }
              }
          }
      }
      
      • 最好将代码移至 Load 事件处理程序。
      • 确保列表视图的属性View 设置为Details

      【讨论】:

      • 非常感谢!
      猜你喜欢
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      相关资源
      最近更新 更多