程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 使用C#和Excel進行報表開發(5)

使用C#和Excel進行報表開發(5)

編輯:關於C#

在用Excel作報表的時候,可能需要操作單元格的邊框和填充顏色和紋理等操作,下面的代碼說明如何設置選中的單元格的填充紋理和邊框。

try
{
      ThisApplication = new Excel.Application();
      ThisWorkbook = ThisApplication.Workbooks.Open("z:\\Book1.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
       ThisApplication.DisplayAlerts = false;
       xlSheet = (Excel.Worksheet)ThisWorkbook.Worksheets.get_Item(1);
       Excel.Range range = xlSheet.get_Range("G4","H5");
       range.Value = "123";
       Excel.Style st = ThisWorkbook.Styles.Add("PropertyBorder", Type.Missing);
       range.Interior.Pattern = Excel.XlPattern.xlPatternCrissCross;
       range.Borders.Weight = 2;
       range.Borders.get_Item(XlBordersIndex.xlEdgeRight).LineStyle = Excel.XlLineStyle.xlContinuous;
       range.Borders.get_Item(XlBordersIndex.xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlContinuous;
       range.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = Excel.XlLineStyle.xlContinuous;
       range.Borders.get_Item(XlBordersIndex.xlDiagonalDown).LineStyle = Excel.XlLineStyle.xlLineStyleNone;
       range.Borders.get_Item(XlBordersIndex.xlDiagonalUp).LineStyle = Excel.XlLineStyle.xlLineStyleNone;
       range.Borders.get_Item(XlBordersIndex.xlInsideHorizontal).LineStyle = Excel.XlLineStyle.xlLineStyleNone;
       range.Borders.get_Item(XlBordersIndex.xlInsideVertical).LineStyle = Excel.XlLineStyle.xlLineStyleNone;
       range.Borders.get_Item(XlBordersIndex.xlEdgeLeft).LineStyle = Excel.XlLineStyle.xlDot;
       range.Borders.get_Item(XlBordersIndex.xlEdgeLeft).Color = System.Drawing.ColorTranslator.ToOle(Color.Red);
       ThisWorkbook.SaveAs("z:\\Book2.xls", Type.Missing, Type.Missing,
              Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
              Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
catch (Exception ex)
{
       MessageBox.Show(ex.Message);
}
finally
{
       ThisWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
       ThisApplication.Workbooks.Close();
       ThisApplication.Quit();
       System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisWorkbook);
       System.Runtime.InteropServices.Marshal.ReleaseComObject(ThisApplication);
       ThisWorkbook = null;
       ThisApplication = null;
       GC.Collect();
       this.Close();
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved