如何将保存在列表中的datagridview复选框列值导出到Excel [英] How to export datagridview checkbox column values saved in a list, into excel

查看:81
本文介绍了如何将保存在列表中的datagridview复选框列值导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个classForm1.在我的class中,我创建了变量来保存数据.

I have a class and a Form1. In my class I have created variables to save my data.

Savestate.cs

public static List<string> plate_list = new List<string>();
public static List<string> checkbox_list = new List<string>();

然后在我的Form1中,通过button1textbox1chk的数据保存到变量中.这两个数据彼此对应.然后我通过button2将它们导出到excel.下面是我的代码:

Then in my Form1, I saved the data of textbox1 and chk into the variables through button1. Both datas are respective of each other. Then I export them into excel through button2. Below is my code:

Form1.cs

private void button1_Click(object sender, EventArgs e)
{
 Savestate.plate_list.Add(textbox1.Text);
 foreach (DataGridViewRow dataGridRow in dataGridView1.Rows)
 {
   DataGridViewCheckBoxCell chk = dataGridRow.Cells[1] as DataGridViewCheckBoxCell;
   if (Convert.ToBoolean(Convert.ToInt32(chk.Value)) == true)
     {
      Savestate. checkbox_list.Add("P");
     }
   else if (Convert.ToBoolean(chk.Value) == false)
     {
      Savestate. checkbox_list.Add("O");
     }
  }
}

//snippet of codes for exporting variables into excel
private void button2_Click(object sender, EventArgs e)
{ 
  int _lastRow1 = oSheet1.Range["C" + oSheet1.Rows.Count].End[Excel.XlDirection.xlUp].Row + 1;
  rng = oSheet1.Range[oSheet1.Cells[_lastRow1, 2], oSheet1.Cells[_lastRow1, 10]];

   foreach (var data in Savestate.plate_list)
        {
          oSheet1.Cells[_lastRow1, 1] = data;
          foreach (var other in Savestate. checkbox_list)
          {
           rng.Value = other;
           (another loop here to read the checkbox list values?)
           }
            _lastRow1++;
        }
 }

使用这些代码,我可以正确循环浏览我的车牌号,但是对于我的复选框值,仅导出最新的值,请参见

With these codes, I am able to loop through my plate number correctly but for my checkbox values, only the latest values are exported, see here (I saved 2 plate numbers so there should be 2 sets of checkbox values but only 1 set of checkbox values are exported). I feel that I have to make another loop specifically for the checkbox values but I don’t know how to start. Hope to get some help. Thanks!

推荐答案

如果执行以下操作,会容易得多:

Is would be a lot easier if you do the following:

  • 在解决方案资源管理器中右键单击该项目

  • Right click the project in solution explorer

选择添加新内容...

Choose Add New...

向项目添加新的数据集

双击数据集,出现类似数据库设计器的内容

Double click the DataSet, something like a database designer appears

右键单击设计图面,选择新建数据表",并为其适当命名

Right click the design surface, choose New DataTable, name it suitably

反复右键单击数据表",选择添加列",然后添加要保留数据的所有列.设置每个列的类型,例如字符串,整数,布尔值等

Right click the DataTable repeatedly, choosing Add Column and add all the columns you want to keep your data in. Set the type of each column e.g. String, Int, Boolean etc

保存数据集

切换回表单设计器,进入相关菜单以显示适用于您的Visual Studio版本的数据源"窗口.如果您不知道如何显示此信息,请使用Google.例如,对于vs 2017:

Switch back to your forms designer, go into the relevant menu to show the "Data Sources" window for your version of visual studio. Google if you don't know how to show this. Example, for vs 2017: https://docs.microsoft.com/en-us/visualstudio/data-tools/add-new-data-sources?view=vs-2017#data-sources-window

在数据源"窗口中是一棵树,其中一个节点是您的DataTable.将其拖到窗体设计器上.出现一个datagridview,其中包含适合数据类型的列.布尔列是一个复选框. datagridview连接到包含数据表实例的DataSet对象.您在网格中键入的任何内容都存储在数据表中.您可以删除不需要的绑定导航器

In the data sources window is a tree and one of the nodes is your DataTable. Drag it onto the forms designer. A datagridview appears with columns suitably types for the data. Bool columns are a checkbox. The datagridview is wired up to a DataSet object that contains an instance of your datatable. Anything you type into the grid is stored in the datatable. You can delete the bindingnavigator that appears if you don't want it

在解决方案资源管理器中右键单击该解决方案,选择管理解决方案的Nuget程序包"

Right click on the solution in solution explorer, choose Manage Nuget Packages For Solution

在掘金包管理器中,单击浏览",然后在搜索框中键入EPPLUS.将epplus库安装到您的项目中.关闭nuget程序包管理器

In the nugget package manager, click Browse and Then type EPPLUS in the search box. Install the epplus library into your project. Close the nuget package manager

双击表单上的保存"按钮.添加〜3行代码,以使epplus从DataTable创建您的excel文件.可以在此处找到更多信息:将数据表导出为使用EPPlus的excel

Double click your save button on your form. Add the ~3 lines of code necessary to have epplus create your excel file from your DataTable. More information can be found here: Export DataTable to excel with EPPlus

在该链接中,您需要将DataTable的名称替换为LoadFromDataTable调用.它位于您的DataSet内,因此类似yourDataSetName.YourDataTabkeName

in that link, you need to substitute the name of your DataTable into the LoadFromDataTable call. It is inside the DataSet on your from, so will be something like yourDataSetName.YourDataTabkeName

以上是称为MVC模型,视图,控制器的概念的实施例.这样做的本质是,您的数据位于名为Model(在此示例中为DataSet/DataTable)的专用容器中,并通过View(数据网格视图)在屏幕上显示,并通过其他方式再次控制/更改(尽管在此示例中)示例,并且在许多情况下,datagridview还是控制器(在某种程度上).用VB6 :)结束在表格上放一个复杂控件(如gridview)并将数据存储在其中的日子已经过去了.尽管有时将数据存储在例如文本框并使用textbox进行挖掘.对于简单场景的文本,这些控件都支持数据绑定,并且如果您的程序是haoldibg多个数据记录并对其进行分页,则意味着所有数据都应存储在模型中,所有显示和编辑它的控件都连接到模型,并且它们都了解在模型中导航的概念-您可以更改模型中引用的当前记录,并且所有文本框等都可以自动更改它们显示的数据.您无需进入每个控件并反复将数据推入其中,而是稍后自己将其挖掘出来

The above is the embodiment of a concept called MVC- model, view, controller. The essence of this is that your data lives in a dedicated container called the Model (the DataSet/DataTable in this example), and it is shown on screen by the View (datagridview) and Controlled/changed by something else again (though in this example and in many, the datagridview is also the controller, to some extent). The days of dropping a complex control on a form, like a gridview, and storing the data inside it were supposed to have been over with VB6 :). Though it is still sometimes more convenient to store data on e.g. Textboxes and dig it out with textbox.Text for simple scenarios, these controls all support data binding and the idea that if your program is haoldibg multiple data records and paging through them, means that all the data should be stored in a model, all the controls that show it and edit it connect to the model and they all understand the concept of navigating through the model- you change the current record your referring to in the model and all the text boxes etc change the data they are showing, automatically. You don't go into each control and push data in there repeatedly, and dig it out later yourself

一旦您了解了MVC范例,它就会使您能够使用其他开发人员也针对它的功能(在本例中为EPPlus),您可以将其交给数据模型(DataSet/DataTable),然后说在这里,将其写到文件",它将执行此操作.您不能将datagridview交给epplus,因为根据MVC原理,不应将数据存储在datagridview中,因此epplus不支持从中挖掘数据

Once you understand the MVC paradigm it opens you up to using functionality where other developers have targeted it too in this case EPPlus, you can just hand it the data model (DataSet/DataTable) and say "here, write this to a file" and it'll do it. You cannot hand your datagridview to epplus, because data is not supposed to be stored in a datagridview according to MVC principles so epplus doesn't support digging data out of it

如果没有别的,希望您能体会到上述步骤虽然在Ive键入时看起来很长,但实际上它所花费的时间更少,鼠标点击次数和代码行数比到目前为止已经编写的要少

If nothing else, hopefully you can appreciate that the above set of steps, though looking long when Ive typed it out, actually involve less time, fewer mouse clicks, and considerably fewer lines of code than you've already written so far.

这篇关于如何将保存在列表中的datagridview复选框列值导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆