我的数据网格视图中有10k行和15列.我想将此数据导出到excel表单o按钮.我已经尝试过以下代码.
private void btExport_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
app.Visible = true;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
for(int i=1;i<dataGridView1.Columns.Count+1;i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i-1].HeaderText;
}
for (int i=0; i < dataGridView1.Rows.Count-1 ; i++)
{
for(int j=0;j<dataGridView1.Columns.Count;j++)
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
else
{
worksheet.Cells[i + 2, j + 1] = "";
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这对我有用, …
我发现了许多类似的问题和答案,但没有人帮助我解决我的问题.
请在下面找到我的dataGridView,

我想要做的是如果名称单元格在运行时为空,则禁用复选框.
我尝试了可能的方法,但在我检查后,单元格一直被禁用(只读).
编辑
我试过这样的事,
private void sendDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (sendDGV.CurrentRow.Cells[1].Value != null)
{
sendDGV.CurrentRow.Cells[2].ReadOnly = false;
sendDGV.Update();
}
else
{
sendDGV.CurrentRow.Cells[2].ReadOnly = true;
sendDGV.Update();
}
}
Run Code Online (Sandbox Code Playgroud) 当处理中文字符(宽)时,我的应用程序在发行版本中表现得很奇怪。我在调试模式下抛出调试断言的行如下:
str.erase(std::remove_if(str.begin(), str.end(), isspace), str.end());
Run Code Online (Sandbox Code Playgroud)
(其中str是std :: wstring)在调试模式下,此行引发断言。我知道这是因为isspace无法处理宽字符。代替isspace,我必须使用iswspace。
str.erase(std::remove_if(str.begin(), str.end(), isspace), str.end());
if (!str.empty())
{ // add str to GUI }
Run Code Online (Sandbox Code Playgroud)
如果我在调试断言中按“ 忽略 ”,则将str正确添加到GUI。但是在发布模式下,str未添加到GUI。
但是,如果我使用iswspace,则将str正确添加到GUI中,而无需对添加到GUI逻辑进行任何更改。
更奇怪的是,在发布模式期间,某些汉字也已正确添加到GUI中。例如,当str为L“?”时,会将str添加到GUI 。。但是当它为L“时,是否未添加到GUI ?。
有人遇到过这个问题吗?
我的理解是处于发布模式,因此不会考虑调试断言,并且其工作方式类似于“ 忽略 ”。
编辑:
我进一步调试了它(在发行版中)。看起来在L“情况下if(!str.empty())不会进入内部吗?” 。但是Visual Studio调试器仍然显示L“?在if条件下达到断点时在str内部。
编辑2:我std::locale::global(std::locale(""));在str.erase行上方添加了内容。现在,它在调试和发布情况下的工作原理完全相同,并且文本已添加到GUI。
这是一个例子:
#include <string>
#include <iostream>
#include <algorithm>
int main(int argc, char* argv[])
{
std::wstring str1 = L"?";
std::wstring …Run Code Online (Sandbox Code Playgroud)