下面代码网上看来的,实在看不出哪里有错,可调试时确实出错,出错行我标示出来了,各位大虾务必帮个忙,先谢一个:
private bool ExportExcel(string FilePath, string p_ReportName)
{
if (this.dataGridView1.Rows.Count == 0)
{
return false;
}
Excel.Application xlApp = new Excel.ApplicationClass();
if(xlApp==null)
{
MessageBox.Show("Excel无法启动");
return false;
} Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];
Excel.Range range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, this.dataGridView1.Columns.Count]);
range.MergeCells = true; //合并单元格
xlApp.ActiveCell.FormulaR1C1 = p_ReportName; //设置标题
xlApp.ActiveCell.Font.Size = 20;
xlApp.ActiveCell.Font.Bold = true;
xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter; //字体对齐方式 int colIndex = 0;
int rowIndex = 0;
int colCount = this.dataGridView1.Columns.Count;
int rowCount = this.dataGridView1.Rows.Count; object[,] objData = new object[rowCount + 1, colCount];
// 获取列标题
foreach (DataGridViewColumn dgvc in this.dataGridView1.Columns)
{
objData[rowIndex, colIndex++] = dgvc.HeaderText;
}
// 获取数据
for (rowIndex = 1; rowIndex <= rowCount; rowIndex++)
{
for (colIndex = 0; colIndex < colCount; colIndex++)
{
objData[rowIndex, colIndex] = this.dataGridView1[rowIndex - 1, colIndex].ToString(); //出错行
}
Application.DoEvents();
}
// 写入Excel
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).Font.Italic = true;
range = xlSheet.get_Range(xlApp.Cells[2, 1], xlApp.Cells[rowCount + 2, colCount]);
range.Value2 = objData; // 保存
try
{
xlApp.Cells.EntireColumn.AutoFit();
xlApp.Cells.VerticalAlignment = Excel.Constants.xlCenter;
xlBook.Saved = true;
xlBook.SaveCopyAs(FilePath + ".xls");
MessageBox.Show("导出成功!");
}
catch
{
MessageBox.Show("保存出错!");
return false;
}
finally
{
xlApp.Quit();
GC.Collect();
KillProcess("excel");
}
return true;
}
出错提示:
索引超出范围。必须为非负值并小于集合大小。
参数名: index
private bool ExportExcel(string FilePath, string p_ReportName)
{
if (this.dataGridView1.Rows.Count == 0)
{
return false;
}
Excel.Application xlApp = new Excel.ApplicationClass();
if(xlApp==null)
{
MessageBox.Show("Excel无法启动");
return false;
} Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];
Excel.Range range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, this.dataGridView1.Columns.Count]);
range.MergeCells = true; //合并单元格
xlApp.ActiveCell.FormulaR1C1 = p_ReportName; //设置标题
xlApp.ActiveCell.Font.Size = 20;
xlApp.ActiveCell.Font.Bold = true;
xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter; //字体对齐方式 int colIndex = 0;
int rowIndex = 0;
int colCount = this.dataGridView1.Columns.Count;
int rowCount = this.dataGridView1.Rows.Count; object[,] objData = new object[rowCount + 1, colCount];
// 获取列标题
foreach (DataGridViewColumn dgvc in this.dataGridView1.Columns)
{
objData[rowIndex, colIndex++] = dgvc.HeaderText;
}
// 获取数据
for (rowIndex = 1; rowIndex <= rowCount; rowIndex++)
{
for (colIndex = 0; colIndex < colCount; colIndex++)
{
objData[rowIndex, colIndex] = this.dataGridView1[rowIndex - 1, colIndex].ToString(); //出错行
}
Application.DoEvents();
}
// 写入Excel
xlApp.get_Range(xlApp.Cells[2, 1], xlApp.Cells[2, colIndex]).Font.Italic = true;
range = xlSheet.get_Range(xlApp.Cells[2, 1], xlApp.Cells[rowCount + 2, colCount]);
range.Value2 = objData; // 保存
try
{
xlApp.Cells.EntireColumn.AutoFit();
xlApp.Cells.VerticalAlignment = Excel.Constants.xlCenter;
xlBook.Saved = true;
xlBook.SaveCopyAs(FilePath + ".xls");
MessageBox.Show("导出成功!");
}
catch
{
MessageBox.Show("保存出错!");
return false;
}
finally
{
xlApp.Quit();
GC.Collect();
KillProcess("excel");
}
return true;
}
出错提示:
索引超出范围。必须为非负值并小于集合大小。
参数名: index
有问题,改成
for (rowIndex = 0; rowIndex < rowCount; rowIndex++)
而dataGridview第0行为表头,肯定会报错
/* 数组和控件行的下标都是从0开始的 */
int colCount = this.dataGridView1.Columns.Count;
int rowCount = this.dataGridView1.Rows.Count;
object[,] objData = new object[rowCount, colCount];
// 获取列标题
foreach (DataGridViewColumn dgvc in this.dataGridView1.Columns)
{
objData[rowIndex, colIndex++] = dgvc.HeaderText;
}
// 获取数据
for (rowIndex = 0; rowIndex < rowCount; rowIndex++)
{
for (colIndex = 0; colIndex < colCount; colIndex++)
{
objData[rowIndex, colIndex] = this.dataGridView1[rowIndex , colIndex].ToString();
}
Application.DoEvents();
}
^_^,给分吧。刚才我在vs上写的时候发现的。
是这句dataGridView1[,]这里面参数写反了。