string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = "abc";
if (saveDialog.ShowDialog() == DialogResult.OK)
{
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,您的电脑可能未安装Excel!");
return;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
if (i + 1 > dataGridView1.ColumnCount)
break;
worksheet.Cells[1, i + 1] = dataGridView1.Columns[i + 1].HeaderText; }
int a = -1;
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (this.dataGridView1.Rows[i].Cells["Column11"].Value == null)
{
continue;
}
if ((bool)this.dataGridView1.Rows[i].Cells["Column11"].Value)
{
a++;
for (int j = 1; j < dataGridView1.ColumnCount; j++)
{
worksheet.Cells[a + 2, j] = dataGridView1.Rows[i].Cells[j].Value;
System.Windows.Forms.Application.DoEvents();
}
}
}
worksheet.Columns.EntireColumn.AutoFit();
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
}
catch (Exception ex)
{
MessageBox.Show("导出文件时出错!");
}
}
xlApp.Quit();
GC.Collect();
MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK);
}
哪位大侠 帮我看看问题出在哪里
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = "abc";
if (saveDialog.ShowDialog() == DialogResult.OK)
{
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return;
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建Excel对象,您的电脑可能未安装Excel!");
return;
}
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; for (int i = 0; i < dataGridView1.ColumnCount; i++)
{
if (i + 1 > dataGridView1.ColumnCount)
break;
worksheet.Cells[1, i + 1] = dataGridView1.Columns[i + 1].HeaderText; }
int a = -1;
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (this.dataGridView1.Rows[i].Cells["Column11"].Value == null)
{
continue;
}
if ((bool)this.dataGridView1.Rows[i].Cells["Column11"].Value)
{
a++;
for (int j = 1; j < dataGridView1.ColumnCount; j++)
{
worksheet.Cells[a + 2, j] = dataGridView1.Rows[i].Cells[j].Value;
System.Windows.Forms.Application.DoEvents();
}
}
}
worksheet.Columns.EntireColumn.AutoFit();
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
}
catch (Exception ex)
{
MessageBox.Show("导出文件时出错!");
}
}
xlApp.Quit();
GC.Collect();
MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK);
}
哪位大侠 帮我看看问题出在哪里
在“COM”选项卡中,选择名为“ActiveMovie 控件类型库”的组件。单击“选择”,然后单击“确定”。重新生成 Interop1。