C# 操作Excel

最近经常写导出报表的软件
所以对Excel操作算是了解的差不多了
拿来与大家分享一下


Excel.Application Excelapp =new Excel.Application();
Excelapp.Visible=false;
Excel._Workbook uniteExcel=null;
try
{
//创建一个Excel
uniteExcel = (Excel._Workbook)(Excelapp.Workbooks.Add( Missing.Value));
//打开一个EXCEL
Excel.Workbook currentExcel= Excelapp.Workbooks.Open("Excel.xls",Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//Excel里的sheet
Excel._Worksheet currentSheet=(Excel._Worksheet)currentExcel.Sheets[1];

uniteSheet1.Cells[1,1]="TEST"//操作表格



//sheet style
uniteSheet1.get_Range(uniteSheet1.Cells[2,1],uniteSheet1.Cells[15,18]).Font.Size=10;

uniteSheet1.get_Range(uniteSheet1.Cells[4,1],uniteSheet1.Cells[4,18]).RowHeight=30;

uniteSheet1.get_Range(uniteSheet1.Cells[3,1],uniteSheet1.Cells[3,18]).RowHeight=30;

uniteSheet1.get_Range(uniteSheet1.Cells[1,1],uniteSheet1.Cells[15,1]).ColumnWidth=5;

uniteSheet1.get_Range(uniteSheet1.Cells[1,2],uniteSheet1.Cells[15,2]).ColumnWidth=15;

uniteSheet1.get_Range(uniteSheet1.Cells[1,11],uniteSheet1.Cells[15,11]).ColumnWidth=10;

uniteSheet1.get_Range(uniteSheet1.Cells[1,14],uniteSheet1.Cells[15,14]).ColumnWidth=10;

uniteSheet1.get_Range(uniteSheet1.Cells[2,3],uniteSheet1.Cells[4,6]).Interior.ColorIndex=27;

uniteSheet1.get_Range(uniteSheet1.Cells[2,7],uniteSheet1.Cells[4,15]).Interior.ColorIndex=38;

uniteSheet1.get_Range(uniteSheet1.Cells[2,16],uniteSheet1.Cells[4,18]).Interior.ColorIndex=35;

uniteSheet1.get_Range(uniteSheet1.Cells[5,1],uniteSheet1.Cells[14,2]).Interior.ColorIndex=33;

uniteSheet1.get_Range(uniteSheet1.Cells[15,1],uniteSheet1.Cells[15,1]).Interior.ColorIndex=33;

uniteSheet1.get_Range(uniteSheet1.Cells[15,2],uniteSheet1.Cells[15,18]).Interior.ColorIndex=15;

uniteSheet1.UsedRange.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

uniteSheet1.UsedRange.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;

uniteSheet1.UsedRange.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin;

uniteSheet1.UsedRange.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

uniteSheet1.UsedRange.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle =Excel.XlLineStyle.xlContinuous;

uniteSheet1.UsedRange.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight =Excel.XlBorderWeight.xlThin;

uniteSheet1.UsedRange.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

uniteSheet1.UsedRange.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle =Excel.XlLineStyle.xlContinuous;

uniteSheet1.UsedRange.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight =Excel.XlBorderWeight.xlThin;

uniteSheet1.UsedRange.Borders[Excel.XlBordersIndex.xlEdgeBottom].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;

uniteSheet1.UsedRange.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle =Excel.XlLineStyle.xlContinuous;

uniteSheet1.UsedRange.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight =Excel.XlBorderWeight.xlThin;


uniteExcel.Saved =false;

uniteExcel.SaveCopyAs(savePath);

}
catch(Exception ex)
{

}
finally
{

System.Runtime.InteropServices.Marshal.ReleaseComObject(uniteSheet1);
System.Runtime.InteropServices.Marshal.ReleaseComObject(uniteSheet2);
System.Runtime.InteropServices.Marshal.ReleaseComObject(uniteSheet3);
//System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
uniteExcel.Close(false,null,null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(uniteExcel);
Excelapp.Quit();
uniteSheet1=null;
uniteSheet2=null;
uniteSheet3=null;

GC.Collect();
GC.WaitForPendingFinalizers();

Excelapp=null;

killExcelProcess()
}

Excel进程每次都关不掉,所以就拿出来杀手锏,不知道这样做好不好,但是肯定的是能用 呵呵
//结束 Excel 进程
foreach(System.Diagnostics.Process xlProcess in System.Diagnostics.Process.GetProcesses())
{
  if( xlProcess.ProcessName.ToUpper().Equals("EXCEL"))
  {
xlProcess.Kill();
   }
}

你可能感兴趣的:(C++,c,Excel,C#)