Microsoft.Office.Interop.Excel.Range range = null;
string saveFileName = "";
bool fileSaved = false;
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.InitialDirectory = @"C:\Documents and Settings\Administrator\桌面";
saveDialog.Filter = "Microsoft.Office.Interop.Excel文件|*.xls";
saveDialog.FileName = fileName;
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("无法创建Microsoft.Office.Interop.Excel对象,可能您的机子未安装Microsoft.Office.Interop.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
//写入标题
int indexOfCol = 0;
for (int i = 0; i < myDGV.ColumnCount; i++)
{
string strColumn=myDGV.Columns[i].Name;
if (strColumn == "PriceId" || strColumn == "FlowerId" || strColumn == "IsValid" || strColumn == "Version" || strColumn == "ValidDateTo" || strColumn == "ValidDateFrom")
{
continue;
}
if (ChargeCell(myDGV.Columns[i].Visible, isShow))
{
indexOfCol = indexOfCol + 1;
worksheet.Cells[1, indexOfCol] = myDGV.Columns[i].HeaderText;
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, indexOfCol];
range.Interior.ColorIndex = 15;//背景颜色
range.Font.Bold = true;//粗体
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;//居中
//加边框
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
range.ColumnWidth = 4.63;//设置列宽
range.EntireColumn.AutoFit();//自动调整列宽
}
}
//写入数值
for (int r = 0; r < myDGV.Rows.Count; r++)
{
indexOfCol = 0;
for (int i = 0; i < myDGV.ColumnCount; i++)
{
if (ChargeCell(myDGV.Columns[i].Visible, isShow))
{
indexOfCol = indexOfCol + 1;
worksheet.Cells[r + 2, indexOfCol] = myDGV.Rows[r].Cells[i].Value;
range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[r + 2, indexOfCol];
range.Interior.ColorIndex = 19;//背景颜色
range.Font.Bold = false;//粗体
range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;//居中
//加边框
range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
range.ColumnWidth = 14.63;//设置列宽
range.EntireColumn.AutoFit();//自动调整列宽
}
}
System.Windows.Forms.Application.DoEvents();
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
if (saveFileName != "")
{
try
{
workbook.Saved = true;
workbook.SaveCopyAs(saveFileName);
fileSaved = true;
}
catch (Exception ex)
{
fileSaved = false;
MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}
}
else
{
fileSaved = false;
}
xlApp.Quit();
GC.Collect();//强行销毁
if (fileSaved && isOpen && System.IO.File.Exists(saveFileName))
System.Diagnostics.Process.Start(saveFileName); //打开EXCEL
MessageBox.Show(fileName + "保存成功", "提示", MessageBoxButtons.OK);