先看一下我抄来的代码,中间修改了一点点。代码是之前baidu得到的,如果作者是您,而且您很生气,就提示我一下,我会很快删除的。

方法2#region 方法2
DataSet ds = ((DataTable)dataGridview1.DataSource).DataSet;//取得dataGrid绑定的DataSet
if (ds == null) return;
string saveFileName = "";
bool fileSaved = false;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = "Sheet1";
saveDialog.ShowDialog();
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];//取得sheet1
Microsoft.Office.Interop.Excel.Range range;
// string oldCaption=this.CaptionText;
long totalCount = ds.Tables[0].Rows.Count;
long rowRead = 0;
float percent = 0;
// worksheet.Cells[1,1]=this.CaptionText;
//写入字段
for (int i = 0; i < dataGridview1.Columns.Count; i++)

{
worksheet.Cells[1, i + 1] = dataGridview1.Columns[i].HeaderText;
//.Tables[0].Columns[i].ColumnName;
//worksheet.Cells[2,i+1]=ds.Tables[0].Columns[i].ColumnName;
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1];
range.Interior.ColorIndex = 15;
range.Font.Bold = true;
}
//写入数值
//this.CaptionVisible = true;
for (int r = 0; r < dataGridview1.Rows.Count; r++)

{
for (int i = 0; i < dataGridview1.Columns.Count; i++)

{
worksheet.Cells[r + 2, i + 1] = dataGridview1.Rows[r].Cells[i].Value.ToString();
}
rowRead++;
percent = ((float)(100 * rowRead)) / totalCount;
this.Text = "正在导出数据[" + percent.ToString("0.00") + "%]
";
Application.DoEvents();
}
// this.CaptionVisible = false;
// this.CaptionText = oldCaption;
range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[dataGridview1.Rows.Count + 1, dataGridview1.Columns.Count]);
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
//range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
//range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous;
//range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
if (ds.Tables[0].Columns.Count > 1)

{
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
}
if (saveFileName != "")

{
try

{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
fileSaved = true;
}
catch (Exception exc)

{
fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + exc.Message);
PCS.BLL.COMM002.OutputText2solone("导出文件时出错,文件可能正被打开", exc);
}
}
else

{
fileSaved = false;
}
xlApp.Quit();
GC.Collect();//强行销毁
// if (fileSaved && File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName);
#endregion
其实早已经实现了此功能,只是有不少缺点,如,我有一列是时间,因为单元格小于时间的长度 导出的文件就显示##########,双击一下就显示正常。
最头疼的就是,我前些天赶时髦,安装了office2007,然后代码就提示了一些错误,就是excel的一些com组件引用错误,我修改后导致没有安装2007的同事无法正常运行。
如果您可以指点一二,请留言给我,不胜感激

方法2#region 方法2
DataSet ds = ((DataTable)dataGridview1.DataSource).DataSet;//取得dataGrid绑定的DataSet
if (ds == null) return;
string saveFileName = "";
bool fileSaved = false;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel文件|*.xls";
saveDialog.FileName = "Sheet1";
saveDialog.ShowDialog();
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];//取得sheet1
Microsoft.Office.Interop.Excel.Range range;
// string oldCaption=this.CaptionText;
long totalCount = ds.Tables[0].Rows.Count;
long rowRead = 0;
float percent = 0;
// worksheet.Cells[1,1]=this.CaptionText;
//写入字段
for (int i = 0; i < dataGridview1.Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = dataGridview1.Columns[i].HeaderText;
//.Tables[0].Columns[i].ColumnName;
//worksheet.Cells[2,i+1]=ds.Tables[0].Columns[i].ColumnName;
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1];
range.Interior.ColorIndex = 15;
range.Font.Bold = true;
}
//写入数值
//this.CaptionVisible = true;
for (int r = 0; r < dataGridview1.Rows.Count; r++)
{
for (int i = 0; i < dataGridview1.Columns.Count; i++)
{
worksheet.Cells[r + 2, i + 1] = dataGridview1.Rows[r].Cells[i].Value.ToString();
}
rowRead++;
percent = ((float)(100 * rowRead)) / totalCount;
this.Text = "正在导出数据[" + percent.ToString("0.00") + "%]
";
Application.DoEvents();
}
// this.CaptionVisible = false;
// this.CaptionText = oldCaption;
range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[dataGridview1.Rows.Count + 1, dataGridview1.Columns.Count]);
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
//range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
//range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous;
//range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
if (ds.Tables[0].Columns.Count > 1)
{
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin;
}
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
fileSaved = true;
}
catch (Exception exc)
{
fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + exc.Message);
PCS.BLL.COMM002.OutputText2solone("导出文件时出错,文件可能正被打开", exc);
}
}
else
{
fileSaved = false;
}
xlApp.Quit();
GC.Collect();//强行销毁
// if (fileSaved && File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName);
#endregion
其实早已经实现了此功能,只是有不少缺点,如,我有一列是时间,因为单元格小于时间的长度 导出的文件就显示##########,双击一下就显示正常。
最头疼的就是,我前些天赶时髦,安装了office2007,然后代码就提示了一些错误,就是excel的一些com组件引用错误,我修改后导致没有安装2007的同事无法正常运行。
如果您可以指点一二,请留言给我,不胜感激
浙公网安备 33010602011771号