protected void lnkBtnCreateWorkbook_Click(object sender, EventArgs e)
{
// get the Excel workbook format
ExcelWorkbookFormat workbookFormat = radioXlsFormat.Checked ? ExcelWorkbookFormat.Xls_2003
: ExcelWorkbookFormat.Xlsx_2007;
// create the workbook in the desired format with a single worksheet
ExcelWorkbook workbook = new ExcelWorkbook(workbookFormat);
// set the license key before saving the workbook
workbook.LicenseKey = "RW51ZXZ0ZXVldGt1ZXZ0a3R3a3x8fHw=";
// set workbook description properties
workbook.DocumentProperties.Subject = "Cell style demo";
workbook.DocumentProperties.Comments = "Add cell styles to an Excel
worksheet using Winnovative Excel library for .NET";
#region CREATE CUSTOM WORKBOOK STYLES
#region Add a style used for the cells in the worksheet title area
ExcelCellStyle titleStyle = workbook.Styles.AddStyle("WorksheetTitleStyle");
// center the text in the title area
titleStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
titleStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
// set the title area borders
titleStyle.Borders[ExcelCellBorderIndex.Bottom].Color = Color.Green;
titleStyle.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Medium;
titleStyle.Borders[ExcelCellBorderIndex.Top].Color = Color.Green;
titleStyle.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Medium;
titleStyle.Borders[ExcelCellBorderIndex.Left].Color = Color.Green;
titleStyle.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Medium;
titleStyle.Borders[ExcelCellBorderIndex.Right].Color = Color.Green;
titleStyle.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Medium;
titleStyle.Borders[ExcelCellBorderIndex.Right].Color = Color.Green;
titleStyle.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Medium;
if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
{
// set the solid fill for the title area range with a custom color
titleStyle.Fill.FillType = ExcelCellFillType.SolidFill;
titleStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
}
else
{
// set the gradient fill for the title area range with a custom color
titleStyle.Fill.FillType = ExcelCellFillType.GradientFill;
titleStyle.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204);
titleStyle.Fill.GradientFillOptions.Color2 = Color.White;
}
// set the title area font
titleStyle.Font.Size = 14;
titleStyle.Font.Bold = true;
titleStyle.Font.UnderlineType = ExcelCellUnderlineType.Single;
#endregion
#region Add a style used for text messages
ExcelCellStyle textMessageStyle = workbook.Styles.AddStyle("TextMessageStyle");
textMessageStyle.Font.Size = 12;
textMessageStyle.Font.Bold = true;
textMessageStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
textMessageStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Left;
textMessageStyle.Fill.FillType = ExcelCellFillType.PatternFill;
textMessageStyle.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_12_5_Percent;
textMessageStyle.Fill.PatternFillOptions.BackColor = Color.White;
textMessageStyle.Fill.PatternFillOptions.PatternColor = Color.Green;
textMessageStyle.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Thin;
textMessageStyle.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Thin;
textMessageStyle.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Thin;
textMessageStyle.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Thin;
#endregion
#region Add a custom cell style
ExcelCellStyle customCellStyle = workbook.Styles.AddStyle("CustomCellStyle");
// center the text in the title area
customCellStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
customCellStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
// set the title area borders
customCellStyle.Borders[ExcelCellBorderIndex.Bottom].Color = Color.Blue;
customCellStyle.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Medium;
customCellStyle.Borders[ExcelCellBorderIndex.Top].Color = Color.Green;
customCellStyle.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Medium;
customCellStyle.Borders[ExcelCellBorderIndex.Left].Color = Color.Red;
customCellStyle.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Medium;
customCellStyle.Borders[ExcelCellBorderIndex.Right].Color = Color.Yellow;
customCellStyle.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Medium;
customCellStyle.Borders[ExcelCellBorderIndex.DiagonalDown].Color = Color.Orange;
customCellStyle.Borders[ExcelCellBorderIndex.DiagonalDown].LineStyle = ExcelCellLineStyle.Medium;
if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
{
// set the solid fill for the title area range with a custom color
customCellStyle.Fill.FillType = ExcelCellFillType.SolidFill;
customCellStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
}
else
{
// set the gradient fill for the title area range with a custom color
customCellStyle.Fill.FillType = ExcelCellFillType.GradientFill;
customCellStyle.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204);
customCellStyle.Fill.GradientFillOptions.Color2 = Color.Orange;
}
// set the title area font
customCellStyle.Font.Size = 14;
customCellStyle.Font.Bold = true;
customCellStyle.Font.UnderlineType = ExcelCellUnderlineType.None;
#endregion
#endregion
// get the first worksheet in the workbook
ExcelWorksheet worksheet = workbook.Worksheets[0];
// set the default worksheet name
worksheet.Name = "Cell Styles Demo";
#region WORKSHEET PAGE SETUP
// set worksheet paper size and orientation, margins, header and footer
worksheet.PageSetup.PaperSize = ExcelPagePaperSize.PaperA4;
worksheet.PageSetup.Orientation = ExcelPageOrientation.Landscape;
worksheet.PageSetup.LeftMargin = 1;
worksheet.PageSetup.RightMargin = 1;
worksheet.PageSetup.TopMargin = 1;
worksheet.PageSetup.BottomMargin = 1;
// add header and footer
//display a logo image in the left part of the header
string imagesPath = System.IO.Path.Combine(Server.MapPath("~"), @"Images");
System.Drawing.Image logoImg = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "logo.jpg"));
worksheet.PageSetup.LeftHeaderFormat = "&G";
worksheet.PageSetup.LeftHeaderPicture = logoImg;
// display worksheet name in the right part of the header
worksheet.PageSetup.RightHeaderFormat = "&A";
// add worksheet header and footer
// display the page number in the center part of the footer
worksheet.PageSetup.CenterFooterFormat = "&P";
// display the workbook file name in the left part of the footer
worksheet.PageSetup.LeftFooterFormat = "&F";
// display the current date in the right part of the footer
worksheet.PageSetup.RightFooterFormat = "&D";
#endregion
#region WRITE THE WORKSHEET TOP TITLE
// merge the cells in the range to create the title area
worksheet["A2:G3"].Merge();
// gets the merged range containing the top left cell of the range
ExcelRange titleRange = worksheet["A2"].MergeArea;
// set the text of title area
worksheet["A2"].Text = "Cell Styles Demo";
// set a row height of 18 points for each row in the range
titleRange.RowHeightInPoints = 18;
// set the worksheet top title style
titleRange.Style = titleStyle;
#endregion
#region SET CELLS WITH VARIOUS VALUES AND FORMATTING
worksheet["A5:C5"].Merge();
worksheet["A5:C5"].Style = textMessageStyle;
worksheet["A5:C5"].RowHeightInPoints = 25;
worksheet["A5"].Value = "Custom Global Style
Demo";
worksheet["A7:G7"].Style = customCellStyle;
worksheet["A9:C9"].Merge();
worksheet["A9:C9"].Style = textMessageStyle;
worksheet["A9:C9"].RowHeightInPoints = 25;
worksheet["A9"].Value = "Solid Fill";
worksheet["A11:G11"].Style.Fill.FillType = ExcelCellFillType.SolidFill;
worksheet["A11:G11"].Style.Fill.SolidFillOptions.BackColor = Color.Orange;
worksheet["A13:C13"].Merge();
worksheet["A13:C13"].Style = textMessageStyle;
worksheet["A13:C13"].RowHeightInPoints = 25;
worksheet["A13"].Value = "Pattern Fill Demo";
worksheet["A15:G15"].Style.Fill.FillType = ExcelCellFillType.PatternFill;
worksheet["A15:G15"].Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_12_5_Percent;
worksheet["A15:G15"].Style.Fill.PatternFillOptions.BackColor = Color.FromArgb(255, 255, 204);
worksheet["A15:G15"].Style.Fill.PatternFillOptions.PatternColor = Color.Orange;
worksheet["A16:G16"].Style.Fill.FillType = ExcelCellFillType.PatternFill;
worksheet["A16:G16"].Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.DiagonalCrosshatch;
worksheet["A16:G16"].Style.Fill.PatternFillOptions.BackColor = Color.FromArgb(255, 255, 204);
worksheet["A16:G16"].Style.Fill.PatternFillOptions.PatternColor = Color.Orange;
worksheet["A17:G17"].Style.Fill.FillType = ExcelCellFillType.PatternFill;
worksheet["A17:G17"].Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.HorizontalStripe;
worksheet["A17:G17"].Style.Fill.PatternFillOptions.BackColor = Color.FromArgb(255, 255, 204);
worksheet["A17:G17"].Style.Fill.PatternFillOptions.PatternColor = Color.Orange;
worksheet["A19:C19"].Merge();
worksheet["A19:C19"].Style = textMessageStyle;
worksheet["A19:C19"].RowHeightInPoints = 25;
worksheet["A19"].Value = "Text Alignment
Demo";
worksheet["A21:G21"].ColumnWidthInChars = 15;
// set row height
worksheet["A21:G21"].RowHeightInPoints = 75;
// set row style
if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
{
// set the solid fill with a custom color
worksheet["A21:G21"].Style.Fill.FillType = ExcelCellFillType.SolidFill;
worksheet["A21:G21"].Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
}
else
{
// set the gradient fill with a custom color
worksheet["A21:G21"].Style.Fill.FillType = ExcelCellFillType.GradientFill;
worksheet["A21:G21"].Style.Fill.GradientFillOptions.Direction = ExcelGradientDirection.DiagonlDown;
worksheet["A21:G21"].Style.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204);
worksheet["A21:G21"].Style.Fill.GradientFillOptions.Color2 = Color.Orange;
}
worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Bottom].Color = Color.DarkBlue;
worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Medium;
worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Top].Color = Color.DarkBlue;
worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Medium;
worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Left].Color = Color.DarkBlue;
worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Medium;
worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Right].Color = Color.DarkBlue;
worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Medium;
worksheet["A21"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Left;
worksheet["A21"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Top;
worksheet["A21"].Style.Font.Bold = true;
worksheet["A21"].Style.Font.Color = Color.Red;
worksheet["A21"].Value = "Top Left";
worksheet["B21"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right;
worksheet["B21"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Bottom;
worksheet["B21"].Style.Font.Bold = true;
worksheet["B21"].Style.Font.Color = Color.Green;
worksheet["B21"].Value = "Bottom Right";
worksheet["C21"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
worksheet["C21"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
worksheet["C21"].Style.Font.Bold = true;
worksheet["C21"].Style.Font.Color = Color.Blue;
worksheet["C21"].Value = "Center Center";
worksheet["D21"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
worksheet["D21"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
worksheet["D21"].Style.Alignment.Orientation = 90;
worksheet["D21"].Style.Font.Bold = true;
worksheet["D21"].Style.Font.Color = Color.Orange;
worksheet["D21"].Value = "Center Vertical";
worksheet["E21"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
worksheet["E21"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Top;
worksheet["E21"].Style.Alignment.Orientation = -90;
worksheet["E21"].Style.Font.Bold = true;
worksheet["E21"].Style.Font.Color = Color.Red;
worksheet["E21"].Value = "Top Vertical";
worksheet["F21"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right;
worksheet["F21"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Top;
worksheet["F21"].Style.Font.Bold = true;
worksheet["F21"].Style.Font.Color = Color.Green;
worksheet["F21"].Value = "Right Top";
worksheet["G21"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right;
worksheet["G21"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Bottom;
worksheet["G21"].Style.Font.Bold = true;
worksheet["G21"].Style.Font.Color = Color.Blue;
worksheet["G21"].Value = "Right Bottom";
worksheet["A23:C23"].Merge();
worksheet["A23:C23"].Style = textMessageStyle;
worksheet["A23:C23"].RowHeightInPoints = 25;
worksheet["A23"].Value = "Fonts Demo";
// set row style
if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
{
// set the solid fill with a custom color
worksheet["A25:G25"].Style.Fill.FillType = ExcelCellFillType.SolidFill;
worksheet["A25:G25"].Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
}
else
{
// set the gradient fill with a custom color
worksheet["A25:G25"].Style.Fill.FillType = ExcelCellFillType.GradientFill;
worksheet["A25:G25"].Style.Fill.GradientFillOptions.Direction = ExcelGradientDirection.DiagonlDown;
worksheet["A25:G25"].Style.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204);
worksheet["A25:G25"].Style.Fill.GradientFillOptions.Color2 = Color.Orange;
}
worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Bottom].Color = Color.DarkBlue;
worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Medium;
worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Top].Color = Color.DarkBlue;
worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Medium;
worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Left].Color = Color.DarkBlue;
worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Medium;
worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Right].Color = Color.DarkBlue;
worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Medium;
worksheet["A25"].Style.Font.Color = Color.Green;
worksheet["A25"].Style.Font.Bold = true;
worksheet["A25"].Style.Font.Size = 14;
worksheet["A25"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
worksheet["A25"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
worksheet["A25"].Value = "Hello";
worksheet["B25"].Style.Font.Color = Color.Blue;
worksheet["B25"].Style.Font.Name = "Verdana";
worksheet["B25"].Style.Font.Bold = true;
worksheet["B25"].Style.Font.Size = 16;
worksheet["B25"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
worksheet["B25"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
worksheet["B25"].Value = "Hello";
worksheet["C25"].Style.Font.Color = Color.Red;
worksheet["C25"].Style.Font.Name = "Times
New Roman";
worksheet["C25"].Style.Font.Bold = true;
worksheet["C25"].Style.Font.Italic = true;
worksheet["C25"].Style.Font.Size = 14;
worksheet["C25"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
worksheet["C25"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
worksheet["C25"].Value = "Hello";
worksheet["D25"].Style.Font.Color = Color.Green;
worksheet["D25"].Style.Font.Bold = true;
worksheet["D25"].Style.Font.Italic = true;
worksheet["D25"].Style.Font.Size = 16;
worksheet["D25"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
worksheet["D25"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
worksheet["D25"].Style.Alignment.Orientation = 90;
worksheet["D25"].Value = "Hello";
worksheet["E25"].Style.Font.Color = Color.Blue;
worksheet["E25"].Style.Font.Bold = true;
worksheet["E25"].Style.Font.Italic = true;
worksheet["E25"].Style.Font.Size = 16;
worksheet["E25"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
worksheet["E25"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
worksheet["E25"].Style.Alignment.Orientation = -90;
worksheet["E25"].Value = "Hello";
worksheet["F25"].Style.Font.Color = Color.Red;
worksheet["F25"].Style.Font.Bold = true;
worksheet["F25"].Style.Font.Italic = true;
worksheet["F25"].Style.Font.UnderlineType = ExcelCellUnderlineType.DoubleAccounting;
worksheet["F25"].Style.Font.Size = 14;
worksheet["F25"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
worksheet["F25"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
worksheet["F25"].Value = "Hello";
worksheet["G25"].Style.Font.Color = Color.Orange;
worksheet["G25"].Style.Font.Bold = true;
worksheet["G25"].Style.Font.IsStrikethrough = true;
worksheet["G25"].Style.Font.Size = 14;
worksheet["G25"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
worksheet["G25"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
worksheet["G25"].Value = "Hello";
worksheet["A27:C27"].Merge();
worksheet["A27:C27"].Style = textMessageStyle;
worksheet["A27:C27"].RowHeightInPoints = 25;
worksheet["A27"].Value = "Borders Demo";
// set row height
worksheet["A29:G29"].RowHeightInPoints = 75;
worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Bottom].Color = Color.DarkBlue;
worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Medium;
worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Top].Color = Color.DarkBlue;
worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Medium;
worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Left].Color = Color.DarkBlue;
worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Medium;
worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Right].Color = Color.DarkBlue;
worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Medium;
// all cell borders have the sae style
worksheet["B29"].Style.Borders.Color = Color.Red;
worksheet["B29"].Style.Borders.LineStyle = ExcelCellLineStyle.DashDot;
worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Bottom].Color = Color.Green;
worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Double;
worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Top].Color = Color.Green;
worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Double;
worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Left].Color = Color.Green;
worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Double;
worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Right].Color = Color.Green;
worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Double;
worksheet["D29"].Style.Borders.Color = Color.Orange;
worksheet["D29"].Style.Borders.LineStyle = ExcelCellLineStyle.Thick;
worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Bottom].Color = Color.Blue;
worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Dot;
worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Top].Color = Color.Blue;
worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Dot;
worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Left].Color = Color.Blue;
worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Dot;
worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Right].Color = Color.Blue;
worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Dot;
worksheet["E29"].Style.Borders[ExcelCellBorderIndex.DiagonalDown].Color = Color.Blue;
worksheet["E29"].Style.Borders[ExcelCellBorderIndex.DiagonalDown].LineStyle = ExcelCellLineStyle.Dot;
worksheet["F29"].Style.Borders.Color = Color.Red;
worksheet["F29"].Style.Borders.LineStyle = ExcelCellLineStyle.MediumDashDot;
worksheet["G29"].Style.Borders.Color = Color.Green;
worksheet["G29"].Style.Borders.LineStyle = ExcelCellLineStyle.Thin;
#endregion
// SAVE THE WORKBOOK
// Save the Excel document in the current HTTP response stream
string outFileName = workbookFormat == ExcelWorkbookFormat.Xls_2003 ? "CellStyles.xls" : "CellStyles.xlsx";
System.Web.HttpResponse httpResponse = System.Web.HttpContext.Current.Response;
// Prepare the HTTP response stream for saving the Excel document
// Clear any data that might have been previously buffered in the
output stream
httpResponse.Clear();
// Set output stream content type for Excel 97-2003 (.xls) or Excel
2007 (.xlsx)
if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
httpResponse.ContentType = "Application/x-msexcel";
else
httpResponse.ContentType = "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
// Add the HTTP header to announce the Excel document either as an
attachment or inline
httpResponse.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", outFileName));
// Save the workbook to the current HTTP response output stream
// and close the workbook after save to release all the allocated
resources
try
{
workbook.Save(httpResponse.OutputStream);
}
catch (Exception ex)
{
// report any error that might occur during save
Session["ErrorMessage"] = ex.Message;
Response.Redirect("ErrorPage.aspx");
}
finally
{
// close the workbook and release the allocated resources
workbook.Close();
#region Dispose the Image object
if (logoImg != null)
logoImg.Dispose();
#endregion
}
// End the response and finish the execution of this page
httpResponse.End();
}
Protected Sub lnkBtnCreateWorkbook_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles lnkBtnCreateWorkbook.Click
' get the Excel workbook format
Dim workbookFormat As ExcelWorkbookFormat = ExcelWorkbookFormat.Xls_2003
If radioXlsxFormat.Checked Then
workbookFormat = ExcelWorkbookFormat.Xlsx_2007
End If
' create the workbook in the desired format with a single worksheet
Dim workbook As ExcelWorkbook = New ExcelWorkbook(workbookFormat)
' set the license key before saving the workbook
workbook.LicenseKey = "RW51ZXZ0ZXVldGt1ZXZ0a3R3a3x8fHw="
' set workbook description properties
workbook.DocumentProperties.Subject = "Cell style demo"
workbook.DocumentProperties.Comments = "Add cell styles to an Excel
worksheet using Winnovative Excel library for .NET"
' CREATE CUSTOM WORKBOOK STYLES
' Add a style used for the cells in the worksheet title area
Dim titleStyle As ExcelCellStyle = workbook.Styles.AddStyle("WorksheetTitleStyle")
' center the text in the title area
titleStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
titleStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
' set the title area borders
titleStyle.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.Green
titleStyle.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Medium
titleStyle.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.Green
titleStyle.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Medium
titleStyle.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.Green
titleStyle.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Medium
titleStyle.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.Green
titleStyle.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Medium
If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
' set the solid fill for the title area range with a custom color
titleStyle.Fill.FillType = ExcelCellFillType.SolidFill
titleStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
Else
' set the gradient fill for the title area range with a custom color
titleStyle.Fill.FillType = ExcelCellFillType.GradientFill
titleStyle.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204)
titleStyle.Fill.GradientFillOptions.Color2 = Color.White
End If
' set the title area font
titleStyle.Font.Size = 14
titleStyle.Font.Bold = True
titleStyle.Font.UnderlineType = ExcelCellUnderlineType.Single
' Add a style used for text messages
Dim textMessageStyle As ExcelCellStyle = workbook.Styles.AddStyle("TextMessageStyle")
textMessageStyle.Font.Size = 12
textMessageStyle.Font.Bold = True
textMessageStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
textMessageStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Left
textMessageStyle.Fill.FillType = ExcelCellFillType.PatternFill
textMessageStyle.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_12_5_Percent
textMessageStyle.Fill.PatternFillOptions.BackColor = Color.White
textMessageStyle.Fill.PatternFillOptions.PatternColor = Color.Green
textMessageStyle.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Thin
textMessageStyle.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Thin
textMessageStyle.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Thin
textMessageStyle.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Thin
' Add a custom cell style
Dim customCellStyle As ExcelCellStyle = workbook.Styles.AddStyle("CustomCellStyle")
' center the text in the title area
customCellStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
customCellStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
' set the title area borders
customCellStyle.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.Blue
customCellStyle.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Medium
customCellStyle.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.Green
customCellStyle.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Medium
customCellStyle.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.Red
customCellStyle.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Medium
customCellStyle.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.Yellow
customCellStyle.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Medium
customCellStyle.Borders.Item(ExcelCellBorderIndex.DiagonalDown).Color = Color.Orange
customCellStyle.Borders.Item(ExcelCellBorderIndex.DiagonalDown).LineStyle = ExcelCellLineStyle.Medium
If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
' set the solid fill for the title area range with a custom color
customCellStyle.Fill.FillType = ExcelCellFillType.SolidFill
customCellStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
Else
' set the gradient fill for the title area range with a custom color
customCellStyle.Fill.FillType = ExcelCellFillType.GradientFill
customCellStyle.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204)
customCellStyle.Fill.GradientFillOptions.Color2 = Color.Orange
End If
' set the title area font
customCellStyle.Font.Size = 14
customCellStyle.Font.Bold = True
customCellStyle.Font.UnderlineType = ExcelCellUnderlineType.None
' get the first worksheet in the workbook
Dim worksheet As ExcelWorksheet = workbook.Worksheets.Item(0)
' set the default worksheet name
worksheet.Name = "Cell Styles Demo"
' WORKSHEET PAGE SETUP
' set worksheet paper size and orientation, margins, header and footer
worksheet.PageSetup.PaperSize = ExcelPagePaperSize.PaperA4
worksheet.PageSetup.Orientation = ExcelPageOrientation.Landscape
worksheet.PageSetup.LeftMargin = 1
worksheet.PageSetup.RightMargin = 1
worksheet.PageSetup.TopMargin = 1
worksheet.PageSetup.BottomMargin = 1
' add header and footer
' display a logo image in the left part of the header
Dim imagesPath As String = System.IO.Path.Combine(Server.MapPath("~"), "Images")
Dim logoImg As System.Drawing.Image = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "logo.jpg"))
worksheet.PageSetup.LeftHeaderFormat = "&G"
worksheet.PageSetup.LeftHeaderPicture = logoImg
' display worksheet name in the right part of the header
worksheet.PageSetup.RightHeaderFormat = "&A"
' add worksheet header and footer
' display the page number in the center part of the footer
worksheet.PageSetup.CenterFooterFormat = "&P"
' display the workbook file name in the left part of the footer
worksheet.PageSetup.LeftFooterFormat = "&F"
' display the current date in the right part of the footer
worksheet.PageSetup.RightFooterFormat = "&D"
' WRITE THE WORKSHEET TOP TITLE
' merge the cells in the range to create the title area
worksheet.Item("A2:G3").Merge()
' gets the merged range containing the top left cell of the range
Dim titleRange As ExcelRange = worksheet.Item("A2").MergeArea
' set the text of title area
worksheet.Item("A2").Text = "Cell Styles
Demo"
' set a row height of 18 points for each row in the range
titleRange.RowHeightInPoints = 18
' set the worksheet top title style
titleRange.Style = titleStyle
' SET CELLS WITH VARIOUS VALUES AND FORMATTING
worksheet.Item("A5:C5").Merge()
worksheet.Item("A5:C5").Style = textMessageStyle
worksheet.Item("A5:C5").RowHeightInPoints = 25
worksheet.Item("A5").Value = "Custom Global
Style Demo"
worksheet.Item("A7:G7").Style = customCellStyle
worksheet.Item("A9:C9").Merge()
worksheet.Item("A9:C9").Style = textMessageStyle
worksheet.Item("A9:C9").RowHeightInPoints = 25
worksheet.Item("A9").Value = "Solid Fill"
worksheet.Item("A11:G11").Style.Fill.FillType = ExcelCellFillType.SolidFill
worksheet.Item("A11:G11").Style.Fill.SolidFillOptions.BackColor = Color.Orange
worksheet.Item("A13:C13").Merge()
worksheet.Item("A13:C13").Style = textMessageStyle
worksheet.Item("A13:C13").RowHeightInPoints = 25
worksheet.Item("A13").Value = "Pattern Fill
Demo"
worksheet.Item("A15:G15").Style.Fill.FillType = ExcelCellFillType.PatternFill
worksheet.Item("A15:G15").Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_12_5_Percent
worksheet.Item("A15:G15").Style.Fill.PatternFillOptions.BackColor = Color.FromArgb(255, 255, 204)
worksheet.Item("A15:G15").Style.Fill.PatternFillOptions.PatternColor = Color.Orange
worksheet.Item("A16:G16").Style.Fill.FillType = ExcelCellFillType.PatternFill
worksheet.Item("A16:G16").Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.DiagonalCrosshatch
worksheet.Item("A16:G16").Style.Fill.PatternFillOptions.BackColor = Color.FromArgb(255, 255, 204)
worksheet.Item("A16:G16").Style.Fill.PatternFillOptions.PatternColor = Color.Orange
worksheet.Item("A17:G17").Style.Fill.FillType = ExcelCellFillType.PatternFill
worksheet.Item("A17:G17").Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.HorizontalStripe
worksheet.Item("A17:G17").Style.Fill.PatternFillOptions.BackColor = Color.FromArgb(255, 255, 204)
worksheet.Item("A17:G17").Style.Fill.PatternFillOptions.PatternColor = Color.Orange
worksheet.Item("A19:C19").Merge()
worksheet.Item("A19:C19").Style = textMessageStyle
worksheet.Item("A19:C19").RowHeightInPoints = 25
worksheet.Item("A19").Value = "Text Alignment
Demo"
worksheet.Item("A21:G21").ColumnWidthInChars = 15
' set row height
worksheet.Item("A21:G21").RowHeightInPoints = 75
' set row style
If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
' set the solid fill with a custom color
worksheet.Item("A21:G21").Style.Fill.FillType = ExcelCellFillType.SolidFill
worksheet.Item("A21:G21").Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
Else
' set the gradient fill with a custom color
worksheet.Item("A21:G21").Style.Fill.FillType = ExcelCellFillType.GradientFill
worksheet.Item("A21:G21").Style.Fill.GradientFillOptions.Direction = ExcelGradientDirection.DiagonlDown
worksheet.Item("A21:G21").Style.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204)
worksheet.Item("A21:G21").Style.Fill.GradientFillOptions.Color2 = Color.Orange
End If
worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.DarkBlue
worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Medium
worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.DarkBlue
worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Medium
worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.DarkBlue
worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Medium
worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.DarkBlue
worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Medium
worksheet.Item("A21").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Left
worksheet.Item("A21").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Top
worksheet.Item("A21").Style.Font.Bold = True
worksheet.Item("A21").Style.Font.Color = Color.Red
worksheet.Item("A21").Value = "Top Left"
worksheet.Item("B21").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right
worksheet.Item("B21").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Bottom
worksheet.Item("B21").Style.Font.Bold = True
worksheet.Item("B21").Style.Font.Color = Color.Green
worksheet.Item("B21").Value = "Bottom Right"
worksheet.Item("C21").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
worksheet.Item("C21").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
worksheet.Item("C21").Style.Font.Bold = True
worksheet.Item("C21").Style.Font.Color = Color.Blue
worksheet.Item("C21").Value = "Center Center"
worksheet.Item("D21").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
worksheet.Item("D21").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
worksheet.Item("D21").Style.Alignment.Orientation = 90
worksheet.Item("D21").Style.Font.Bold = True
worksheet.Item("D21").Style.Font.Color = Color.Orange
worksheet.Item("D21").Value = "Center Vertical"
worksheet.Item("E21").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
worksheet.Item("E21").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Top
worksheet.Item("E21").Style.Alignment.Orientation = -90
worksheet.Item("E21").Style.Font.Bold = True
worksheet.Item("E21").Style.Font.Color = Color.Red
worksheet.Item("E21").Value = "Top Vertical"
worksheet.Item("F21").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right
worksheet.Item("F21").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Top
worksheet.Item("F21").Style.Font.Bold = True
worksheet.Item("F21").Style.Font.Color = Color.Green
worksheet.Item("F21").Value = "Right Top"
worksheet.Item("G21").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right
worksheet.Item("G21").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Bottom
worksheet.Item("G21").Style.Font.Bold = True
worksheet.Item("G21").Style.Font.Color = Color.Blue
worksheet.Item("G21").Value = "Right Bottom"
worksheet.Item("A23:C23").Merge()
worksheet.Item("A23:C23").Style = textMessageStyle
worksheet.Item("A23:C23").RowHeightInPoints = 25
worksheet.Item("A23").Value = "Fonts Demo"
' set row style
If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
' set the solid fill with a custom color
worksheet.Item("A25:G25").Style.Fill.FillType = ExcelCellFillType.SolidFill
worksheet.Item("A25:G25").Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
Else
' set the gradient fill with a custom color
worksheet.Item("A25:G25").Style.Fill.FillType = ExcelCellFillType.GradientFill
worksheet.Item("A25:G25").Style.Fill.GradientFillOptions.Direction = ExcelGradientDirection.DiagonlDown
worksheet.Item("A25:G25").Style.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204)
worksheet.Item("A25:G25").Style.Fill.GradientFillOptions.Color2 = Color.Orange
End If
worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.DarkBlue
worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Medium
worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.DarkBlue
worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Medium
worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.DarkBlue
worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Medium
worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.DarkBlue
worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Medium
worksheet.Item("A25").Style.Font.Color = Color.Green
worksheet.Item("A25").Style.Font.Bold = True
worksheet.Item("A25").Style.Font.Size = 14
worksheet.Item("A25").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
worksheet.Item("A25").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
worksheet.Item("A25").Value = "Hello"
worksheet.Item("B25").Style.Font.Color = Color.Blue
worksheet.Item("B25").Style.Font.Name = "Verdana"
worksheet.Item("B25").Style.Font.Bold = True
worksheet.Item("B25").Style.Font.Size = 16
worksheet.Item("B25").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
worksheet.Item("B25").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
worksheet.Item("B25").Value = "Hello"
worksheet.Item("C25").Style.Font.Color = Color.Red
worksheet.Item("C25").Style.Font.Name = "Times
New Roman"
worksheet.Item("C25").Style.Font.Bold = True
worksheet.Item("C25").Style.Font.Italic = True
worksheet.Item("C25").Style.Font.Size = 14
worksheet.Item("C25").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
worksheet.Item("C25").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
worksheet.Item("C25").Value = "Hello"
worksheet.Item("D25").Style.Font.Color = Color.Green
worksheet.Item("D25").Style.Font.Bold = True
worksheet.Item("D25").Style.Font.Italic = True
worksheet.Item("D25").Style.Font.Size = 16
worksheet.Item("D25").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
worksheet.Item("D25").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
worksheet.Item("D25").Style.Alignment.Orientation = 90
worksheet.Item("D25").Value = "Hello"
worksheet.Item("E25").Style.Font.Color = Color.Blue
worksheet.Item("E25").Style.Font.Bold = True
worksheet.Item("E25").Style.Font.Italic = True
worksheet.Item("E25").Style.Font.Size = 16
worksheet.Item("E25").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
worksheet.Item("E25").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
worksheet.Item("E25").Style.Alignment.Orientation = -90
worksheet.Item("E25").Value = "Hello"
worksheet.Item("F25").Style.Font.Color = Color.Red
worksheet.Item("F25").Style.Font.Bold = True
worksheet.Item("F25").Style.Font.Italic = True
worksheet.Item("F25").Style.Font.UnderlineType = ExcelCellUnderlineType.DoubleAccounting
worksheet.Item("F25").Style.Font.Size = 14
worksheet.Item("F25").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
worksheet.Item("F25").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
worksheet.Item("F25").Value = "Hello"
worksheet.Item("G25").Style.Font.Color = Color.Orange
worksheet.Item("G25").Style.Font.Bold = True
worksheet.Item("G25").Style.Font.IsStrikethrough = True
worksheet.Item("G25").Style.Font.Size = 14
worksheet.Item("G25").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
worksheet.Item("G25").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
worksheet.Item("G25").Value = "Hello"
worksheet.Item("A27:C27").Merge()
worksheet.Item("A27:C27").Style = textMessageStyle
worksheet.Item("A27:C27").RowHeightInPoints = 25
worksheet.Item("A27").Value = "Borders Demo"
' set row height
worksheet.Item("A29:G29").RowHeightInPoints = 75
worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.DarkBlue
worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Medium
worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.DarkBlue
worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Medium
worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.DarkBlue
worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Medium
worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.DarkBlue
worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Medium
' all cell borders have the sae style
worksheet.Item("B29").Style.Borders.Color = Color.Red
worksheet.Item("B29").Style.Borders.LineStyle = ExcelCellLineStyle.DashDot
worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.Green
worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Double
worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.Green
worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Double
worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.Green
worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Double
worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.Green
worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Double
worksheet.Item("D29").Style.Borders.Color = Color.Orange
worksheet.Item("D29").Style.Borders.LineStyle = ExcelCellLineStyle.Thick
worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.Blue
worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Dot
worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.Blue
worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Dot
worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.Blue
worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Dot
worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.Blue
worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Dot
worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.DiagonalDown).Color = Color.Blue
worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.DiagonalDown).LineStyle = ExcelCellLineStyle.Dot
worksheet.Item("F29").Style.Borders.Color = Color.Red
worksheet.Item("F29").Style.Borders.LineStyle = ExcelCellLineStyle.MediumDashDot
worksheet.Item("G29").Style.Borders.Color = Color.Green
worksheet.Item("G29").Style.Borders.LineStyle = ExcelCellLineStyle.Thin
' SAVE THE WORKBOOK
' Save the Excel document in the current HTTP response stream
Dim outFileName As String
If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
outFileName = "CellStyles.xls"
Else
outFileName = "CellStyles.xlsx"
End If
Dim httpResponse As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
' Prepare the HTTP response stream for saving the Excel document
' Clear any data that might have been previously buffered in the output
stream
httpResponse.Clear()
' Set output stream content type for Excel 97-2003 (.xls) or Excel
2007 (.xlsx)
If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
httpResponse.ContentType = "Application/x-msexcel"
Else
httpResponse.ContentType = "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
End If
' Add the HTTP header to announce the Excel document either as an
attachment or inline
httpResponse.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", outFileName))
' Save the workbook to the current HTTP response output stream
' and close the workbook after save to release all the allocated resources
Try
workbook.Save(httpResponse.OutputStream)
Catch ex As Exception
' report any error that might occur during save
Session.Item("ErrorMessage") = ex.Message
Response.Redirect("ErrorPage.aspx")
Finally
' close the workbook and release the allocated resources
workbook.Close()
' Dispose the Image object
If Not logoImg Is Nothing Then
logoImg.Dispose()
End If
End Try
' End the response and finish the execution of this page
httpResponse.End()
End Sub
|