积极答复者
将GV导入EXCEL问题

问题
-
{
if (gridView.RowCount == 0) return false;
Application.DoEvents();
// 创建Excel对象
Excel.Application xlApp = new Excel.ApplicationClass();
if (xlApp == null)
{
ClientFuncion.ShowErrorMsg("Excel无法启动");
return false;
}
// 创建Excel工作薄
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, gridView.ColumnCount]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = string.Empty;
xlApp.ActiveCell.Font.Size = 20;
xlApp.ActiveCell.Font.Bold = true;
xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;
// 列索引,行索引,总列数,总行数
int colIndex = 0;
int RowIndex = 0;
int colCount = gridView.ColumnCount;
int RowCount = gridView.BindingContext[gridView.DataSource, gridView.DataMember].Count;
// 创建缓存数据
object[,] objData = new object[RowCount + 1, colCount];
// 获取列标题
foreach (DataGridViewColumn cs in gridView.Columns)
{
objData[RowIndex, colIndex++] = cs.HeaderText;
}
// 获取数据
for (RowIndex = 0; RowIndex < RowCount; RowIndex++)
{
for (colIndex = 0; colIndex < colCount; colIndex++)
{
objData[RowIndex, colIndex] = gridView[colIndex, RowIndex].FormattedValue;
}
Application.DoEvents();
}
// 写入Excel
range = xlSheet.get_Range(xlApp.Cells[2, 1], xlApp.Cells[RowCount, colCount]);
range.Value2 = objData;
// 保存
try
{
xlBook.Saved = true;
xlBook.SaveCopyAs(excleFileName);
}
catch
{
ClientFuncion.ShowErrorMsg("保存出错,请检查!");
return false;
}
finally
{
xlBook = null;
range = null;
xlApp.Quit();
GC.Collect();
}
return true;
}
}
想请教下我保存的文件里面就一列数据显示1字.
可否帮我检查下哪出错了
答案
-
在VS2008和Office 2003下测试没有问题。
另外,如果你想打印表头,应当使用下面的代码,你的代码标题虽然赋值了,但下面的代码覆盖了
Code Snippetpublic static bool ExportExcel(DataGridView gridView, string excleFileName)
{
if (gridView.RowCount == 0) return false;Application.DoEvents();
// 创建Excel对象
Excel.Application xlApp = new Excel.ApplicationClass();
if (xlApp == null)
{
MessageBox.Show("Excel无法启动");
return false;
}
// 创建Excel工作薄
Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];// 列索引,行索引,总列数,总行数
int colIndex = 0;
int RowIndex = 0;
int colCount = gridView.ColumnCount;
int RowCount = gridView.BindingContext[gridView.DataSource, gridView.DataMember].Count;// 设置标题
Excel.Range range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, colCount]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = string.Empty;
xlApp.ActiveCell.Font.Size = 20;
xlApp.ActiveCell.Font.Bold = true;
xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;
range.Value2 = "文件标题";
// 创建缓存数据
object[,] objData = new object[RowCount + 1, colCount];// 获取列标题
foreach (DataGridViewColumn cs in gridView.Columns)
{
objData[RowIndex, colIndex++] = cs.HeaderText;
}// 获取数据
for (RowIndex = 0; RowIndex < RowCount; RowIndex++)
{
for (colIndex = 0; colIndex < colCount; colIndex++)
{
objData[RowIndex + 1, colIndex] = gridView[colIndex, RowIndex].FormattedValue;
}
Application.DoEvents();
}
// 写入Excel
//range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[RowCount+1, colCount]);
range = xlSheet.get_Range(xlApp.Cells[2, 1], xlApp.Cells[RowCount + 2, colCount]);
range.Value2 = objData;// 保存
try
{
xlBook.Saved = true;
xlBook.SaveCopyAs(excleFileName);
}
catch
{
MessageBox.Show("保存出错,请检查!");
return false;
}
finally
{
xlBook = null;
range = null;
xlApp.Quit();
GC.Collect();
}
return true;
} -
因为你的数组中包括一行标题,以及你从Excel第二行开始填充数据导致的一些索引问题。
黄底红字部分为你需要修改的地方:
Code Snippetpublic static bool ExportExcel(DataGridView gridView, string excleFileName)
{
if (gridView.RowCount == 0) return false;
Application.DoEvents();
// 创建Excel对象
Excel.Application xlApp = new Excel.ApplicationClass();
if (xlApp == null)
{
ClientFuncion.ShowErrorMsg("Excel无法启动");
return false;
}
// 创建Excel工作薄
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, gridView.ColumnCount]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = string.Empty;
xlApp.ActiveCell.Font.Size = 20;
xlApp.ActiveCell.Font.Bold = true;
xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;
// 列索引,行索引,总列数,总行数
int colIndex = 0;
int RowIndex = 0;
int colCount = gridView.ColumnCount;
int RowCount = gridView.BindingContext[gridView.DataSource, gridView.DataMember].Count;
// 创建缓存数据
object[,] objData = new object[RowCount + 1, colCount];
// 获取列标题
foreach (DataGridViewColumn cs in gridView.Columns)
{
objData[RowIndex, colIndex++] = cs.HeaderText;
}
// 获取数据
for (RowIndex = 0; RowIndex < RowCount; RowIndex++)
{
for (colIndex = 0; colIndex < colCount; colIndex++)
{
objData[RowIndex + 1, colIndex] = gridView[colIndex, RowIndex].FormattedValue;
}
Application.DoEvents();
}
// 写入Excel
range = xlSheet.get_Range(xlApp.Cells[2, 1], xlApp.Cells[RowCount+2, colCount]);
range.Value2 = objData;
// 保存
try
{
xlBook.Saved = true;
xlBook.SaveCopyAs(excleFileName);
}
catch
{
ClientFuncion.ShowErrorMsg("保存出错,请检查!");
return false;
}
finally
{
xlBook = null;
range = null;
xlApp.Quit();
GC.Collect();
}
return true;
}
}
全部回复
-
在VS2008和Office 2003下测试没有问题。
另外,如果你想打印表头,应当使用下面的代码,你的代码标题虽然赋值了,但下面的代码覆盖了
Code Snippetpublic static bool ExportExcel(DataGridView gridView, string excleFileName)
{
if (gridView.RowCount == 0) return false;Application.DoEvents();
// 创建Excel对象
Excel.Application xlApp = new Excel.ApplicationClass();
if (xlApp == null)
{
MessageBox.Show("Excel无法启动");
return false;
}
// 创建Excel工作薄
Excel.Workbook xlBook = xlApp.Workbooks.Add(true);
Excel.Worksheet xlSheet = (Excel.Worksheet)xlBook.Worksheets[1];// 列索引,行索引,总列数,总行数
int colIndex = 0;
int RowIndex = 0;
int colCount = gridView.ColumnCount;
int RowCount = gridView.BindingContext[gridView.DataSource, gridView.DataMember].Count;// 设置标题
Excel.Range range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[1, colCount]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = string.Empty;
xlApp.ActiveCell.Font.Size = 20;
xlApp.ActiveCell.Font.Bold = true;
xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;
range.Value2 = "文件标题";
// 创建缓存数据
object[,] objData = new object[RowCount + 1, colCount];// 获取列标题
foreach (DataGridViewColumn cs in gridView.Columns)
{
objData[RowIndex, colIndex++] = cs.HeaderText;
}// 获取数据
for (RowIndex = 0; RowIndex < RowCount; RowIndex++)
{
for (colIndex = 0; colIndex < colCount; colIndex++)
{
objData[RowIndex + 1, colIndex] = gridView[colIndex, RowIndex].FormattedValue;
}
Application.DoEvents();
}
// 写入Excel
//range = xlSheet.get_Range(xlApp.Cells[1, 1], xlApp.Cells[RowCount+1, colCount]);
range = xlSheet.get_Range(xlApp.Cells[2, 1], xlApp.Cells[RowCount + 2, colCount]);
range.Value2 = objData;// 保存
try
{
xlBook.Saved = true;
xlBook.SaveCopyAs(excleFileName);
}
catch
{
MessageBox.Show("保存出错,请检查!");
return false;
}
finally
{
xlBook = null;
range = null;
xlApp.Quit();
GC.Collect();
}
return true;
} -
因为你的数组中包括一行标题,以及你从Excel第二行开始填充数据导致的一些索引问题。
黄底红字部分为你需要修改的地方:
Code Snippetpublic static bool ExportExcel(DataGridView gridView, string excleFileName)
{
if (gridView.RowCount == 0) return false;
Application.DoEvents();
// 创建Excel对象
Excel.Application xlApp = new Excel.ApplicationClass();
if (xlApp == null)
{
ClientFuncion.ShowErrorMsg("Excel无法启动");
return false;
}
// 创建Excel工作薄
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, gridView.ColumnCount]);
range.MergeCells = true;
xlApp.ActiveCell.FormulaR1C1 = string.Empty;
xlApp.ActiveCell.Font.Size = 20;
xlApp.ActiveCell.Font.Bold = true;
xlApp.ActiveCell.HorizontalAlignment = Excel.Constants.xlCenter;
// 列索引,行索引,总列数,总行数
int colIndex = 0;
int RowIndex = 0;
int colCount = gridView.ColumnCount;
int RowCount = gridView.BindingContext[gridView.DataSource, gridView.DataMember].Count;
// 创建缓存数据
object[,] objData = new object[RowCount + 1, colCount];
// 获取列标题
foreach (DataGridViewColumn cs in gridView.Columns)
{
objData[RowIndex, colIndex++] = cs.HeaderText;
}
// 获取数据
for (RowIndex = 0; RowIndex < RowCount; RowIndex++)
{
for (colIndex = 0; colIndex < colCount; colIndex++)
{
objData[RowIndex + 1, colIndex] = gridView[colIndex, RowIndex].FormattedValue;
}
Application.DoEvents();
}
// 写入Excel
range = xlSheet.get_Range(xlApp.Cells[2, 1], xlApp.Cells[RowCount+2, colCount]);
range.Value2 = objData;
// 保存
try
{
xlBook.Saved = true;
xlBook.SaveCopyAs(excleFileName);
}
catch
{
ClientFuncion.ShowErrorMsg("保存出错,请检查!");
return false;
}
finally
{
xlBook = null;
range = null;
xlApp.Quit();
GC.Collect();
}
return true;
}
}