/*  background color and borders in Excel. taken from Example01.cs */ 
public void Example01Main() { // start excel and turn off msg boxes Excel.Application excelApplication = new Excel.Application(); excelApplication.DisplayAlerts = false; // add a new workbook Excel.Workbook workBook = excelApplication.Workbooks.Add(); Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1]; // draw back color and perform the BorderAround method workSheet.Range("$B2:$B5").Interior.Color = ToDouble(Color.DarkGreen); workSheet.Range("$B2:$B5").BorderAround(XlLineStyle.xlContinuous,
XlBorderWeight.xlMedium, XlColorIndex.xlColorIndexAutomatic); // draw back color and border the range explicitly workSheet.Range("$D2:$D5").Interior.Color = ToDouble(Color.DarkGreen); workSheet.Range("$D2:$D5").Borders[XlBordersIndex.xlInsideHorizontal].LineStyle
= XlLineStyle.xlDouble;
workSheet.Range("$D2:$D5").Borders[XlBordersIndex.xlInsideHorizontal].Weight = 4;
workSheet.Range("$D2:$D5").Borders[XlBordersIndex.xlInsideHorizontal].Color
= ToDouble(Color.Black); // save the book string fileExtension = GetDefaultExtension(excelApplication); string workbookFile = string.Format("{0}\\Example01{1}",
Application.StartupPath, fileExtension);
workBook.SaveAs(workbookFile, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive); // close excel and dispose reference excelApplication.Quit(); excelApplication.Dispose(); } /// <summary> /// Translate a color to double /// </summary> /// <param name="color">expression to convert</param> /// <returns>color</returns> private static double ToDouble(System.Drawing.Color color) { uint returnValue = color.B; returnValue = returnValue << 8; returnValue += color.G; returnValue = returnValue << 8; returnValue += color.R; return returnValue; } /// <summary> /// returns the valid file extension for the instance. for example ".xls" or ".xlsx" /// </summary> /// <param name="application">the instance</param> /// <returns>the extension</returns> private static string GetDefaultExtension(Excel.Application application) { double Version = Convert.ToDouble(application.Version, CultureInfo.InvariantCulture); if (Version >= 12.00) return ".xlsx"; else return ".xls"; }

Last edited May 14, 2012 at 5:17 AM by SebastianDotNet, version 29

Comments

No comments yet.