[英]Exporting DataGridView records to Excel
I have a datagridview with several columns and different report types, where I have a method to export the records to Excel spreadsheet, in this datagridview I leave some columns like: visible = false
according to the selected report type.我有一个包含多个列和不同报告类型的 datagridview,我有一种方法可以将记录导出到 Excel 电子表格,在这个 datagridview 中,我根据所选的报告类型保留了一些列,例如: visible = false
。
In the export method for spreadsheet I have a validation to consider only visible cells, true but it is not working.在电子表格的导出方法中,我进行了验证以仅考虑可见单元格,为 true 但它不起作用。
int XLRow = 1;
int XLCol = 1;
// Export header
for (int i = 0; i < datagrid.Columns.Count; i++)
{
if (datagrid.Columns[i].Visible == true)
xlWorkSheet.Cells[XLRow, XLCol++] = datagrid.Columns[i].HeaderText;
}
XLRow = 2;
XLCol = 1;
// Controls for scrolling through records do DataGridView
for (int i = 0; i < datagrid.RowCount; i++)
{
for (int j = 0; j < datagrid.ColumnCount; j++)
{
DataGridViewCell cell = datagrid[j, i];
string conteudo = string.Empty;
if ((cell.Value != null) && (!string.IsNullOrEmpty(cell.Value.ToString())) && cell.Visible == true)
{
conteudo = cell.Value.ToString();
if ((Funcoes.EhNumerico(conteudo)) && (conteudo.Length > 8))
{
conteudo = string.Concat("'", conteudo);
}
xlWorkSheet.Cells[XLRow, XLCol++] = conteudo;
}
XLRow++;
XLCol = 1;
}
}
The spreadsheet leaves with the columns that are visible = false
in white, as follows:电子表格留下visible = false
的列visible = false
白色,如下所示:
How can I resolve this?我该如何解决这个问题?
In other words… in the first for
loop… if the column is not visible… then you DO NOT want to increment i
… and this is obviously going to mess up the for
loop.换句话说……在第一个for
循环中……如果该列不可见……那么您不想增加i
……这显然会弄乱for
循环。 Hence my suggestion to create two (2) int
variables… int XLRow
and int XLColumn
, then use those indexes specifically for the WORKSHEET.因此,我建议创建两 (2) 个int
变量…… int XLRow
和int XLColumn
,然后专门为int XLColumn
使用这些索引。 Then loop through the grid columns as your code does using i
and j
, however, when a column is found that is not visible, then you DO NOT want to increment the XLCol
index.然后像您的代码一样使用i
和j
循环遍历网格列,但是,当发现不可见的列时,您不想增加XLCol
索引。
It will be a challenging juggling the loops
i
or j
variables to also be used as an index into the worksheet columns as they may be completely “different” indexes.将loops
i
或j
变量也用作工作表列的索引将是一项具有挑战性的工作,因为它们可能是完全“不同”的索引。 This is why I say “separate” them from the git go and keep it simple.这就是为什么我说将它们从 git go 中“分离”出来并保持简单。 Something like…就像是…
int XLRow = 1;
int XLCol = 1;
for (int i = 0; i < datagrid.Columns.Count; i++) {
if (datagrid.Columns[i].Visible == true)
xlWorkSheet.Cells[XLRow, XLCol++] = datagrid.Columns[i].HeaderText;
}
XLRow = 2;
XLCol = 1;
for (int i = 0; i < datagrid.RowCount; i++) {
for (int j = 0; j < datagrid.ColumnCount; j++) {
if (datagrid.Columns[j].Visible) {
DataGridViewCell cell = datagrid[j, i];
string conteudo = string.Empty;
if ((cell.Value != null) && (!string.IsNullOrEmpty(cell.Value.ToString()))) {
conteudo = cell.Value.ToString();
if ((Funcoes.EhNumerico(conteudo)) && (conteudo.Length > 8)) {
conteudo = string.Concat("'", conteudo);
}
xlWorkSheet.Cells[XLRow, XLCol++] = conteudo;
}
}
}
XLRow++;
XLCol = 1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.