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 = "Data validation sample";
workbook.DocumentProperties.Comments =
"Add data validation 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;
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 = 10;
textMessageStyle.Font.Bold = true;
textMessageStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
textMessageStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right;
textMessageStyle.Fill.FillType = ExcelCellFillType.SolidFill;
textMessageStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(204, 255, 204);
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 style used for data validation ranges
ExcelCellStyle dataValidationStyle = workbook.Styles.AddStyle("DataValidationStyle");
dataValidationStyle.Font.Size = 10;
dataValidationStyle.Font.Bold = true;
dataValidationStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
dataValidationStyle.Fill.FillType = ExcelCellFillType.SolidFill;
dataValidationStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(153, 204, 0);
dataValidationStyle.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Thin;
dataValidationStyle.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Thin;
dataValidationStyle.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Thin;
dataValidationStyle.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Thin;
#endregion
#endregion
// get the first worksheet in the workbook
ExcelWorksheet worksheet = workbook.Worksheets[0];
// set the default worksheet name
worksheet.Name = "Data Validation Demo";
#region WORKSHEET PAGE SETUP
// set worksheet paper size and orientation, margins, header and footer
worksheet.PageSetup.PaperSize = ExcelPagePaperSize.PaperA4;
worksheet.PageSetup.Orientation = ExcelPageOrientation.Portrait;
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 = "Adding Data Validation
to an Excel Worksheet";
// 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 Add Data Validation
#region Validate data from a list
worksheet["A5:E5"].Merge();
worksheet["A5:E5"].Style = textMessageStyle;
worksheet["A5:E5"].Value = "Select a value
from the list:";
// set the range to be validated
worksheet["G5"].Style = dataValidationStyle;
worksheet["G5"].ColumnWidthInChars = 25;
worksheet["G5"].AddComment("Click this cell
to select a value from list.");
ExcelDataValidator listValidator = worksheet["G5"].DataValidator;
listValidator.AllowedDataType = ExcelDataValidatorDataType.List;
listValidator.AllowedValues = new string[] { "HTML to PDF Converter", "PDF Merge",
"PDF Security", "Excel Library for .NET" };
listValidator.InputMessageText = "Select a value from the list";
listValidator.ShowInputMessage = true;
#endregion
#region Validate a whole number between 0 and 10
worksheet["A7:E7"].Merge();
worksheet["A7:E7"].Style = textMessageStyle;
worksheet["A7:E7"].Value = "Enter a whole
number between 0 and 10 :";
// set the range to be validated
worksheet["G7"].Style = dataValidationStyle;
worksheet["G7"].ColumnWidthInChars = 25;
worksheet["G7"].AddComment("Click this cell
to enter a whole number.");
// Data Validation for Numbers
ExcelDataValidator wholeNumberValidator = worksheet["G7"].DataValidator;
wholeNumberValidator.AllowedDataType = ExcelDataValidatorDataType.WholeNumber;
wholeNumberValidator.Operator = ExcelDataValidatorOperator.Between;
wholeNumberValidator.Value1 = 0;
wholeNumberValidator.Value2 = 10;
wholeNumberValidator.ErrorAlertText = "A number between 0 to 10 is
expected";
wholeNumberValidator.ShowErrorAlert = true;
wholeNumberValidator.ErrorAlertTitle = "Whole Number Validation Error";
wholeNumberValidator.InputMessageText = "Enter a whole number between
0 and 10";
wholeNumberValidator.ShowInputMessage = true;
#endregion
#region Validate a date between 01/01/2000 and 12/31/2009
worksheet["A9:E9"].Merge();
worksheet["A9:E9"].Style = textMessageStyle;
worksheet["A9:E9"].Value = "Enter a date
between 01/01/2000 and 12/31/2009 :";
// set the range to be validated
worksheet["G9"].Style = dataValidationStyle;
worksheet["G9"].ColumnWidthInChars = 25;
worksheet["G9"].Style.Number.NumberFormatString = "m/d/yyyy";
worksheet["G9"].Value = new DateTime(2008, 12, 15); // default value
worksheet["G9"].AddComment("Double-Click
this cell to enter a date in local format.");
ExcelDataValidator dateValidator = worksheet["G9"].DataValidator;
dateValidator.AllowedDataType = ExcelDataValidatorDataType.Date;
dateValidator.Operator = ExcelDataValidatorOperator.Between;
dateValidator.Value1 = new DateTime(2000, 1, 1);
dateValidator.Value2 = new DateTime(2009, 12, 31); ;
dateValidator.ErrorAlertText = "A date between 01/01/2000 and 12/31/2009
is expected";
dateValidator.ShowErrorAlert = true;
dateValidator.ErrorAlertTitle = "Date Validation Error";
dateValidator.InputMessageText = "Enter a date between 01/01/2000
and 12/31/2009";
dateValidator.ShowInputMessage = true;
#endregion
#region Validate the length of a text
worksheet["A11:E11"].Merge();
worksheet["A11:E11"].Style = textMessageStyle;
worksheet["A11:E11"].Value = "Enter a text
with length between 2 and 5 chars:";
// set the range to be validated
worksheet["G11"].Style = dataValidationStyle;
worksheet["G11"].ColumnWidthInChars = 25;
worksheet["G11"].AddComment("Click this cell
to enter a text.");
ExcelDataValidator textLengthValidator = worksheet["G11"].DataValidator;
textLengthValidator.AllowedDataType = ExcelDataValidatorDataType.TextLength;
textLengthValidator.Operator = ExcelDataValidatorOperator.Between;
textLengthValidator.Value1 = 2;
textLengthValidator.Value2 = 5;
textLengthValidator.ErrorAlertTitle = "Text Length Validation Error";
textLengthValidator.ErrorAlertText = "A text with length between 2
and 5 chars is expected";
textLengthValidator.ShowErrorAlert = true;
textLengthValidator.InputMessageText = "Enter a text with length between
2 and 5 characters";
textLengthValidator.ShowInputMessage = true;
#endregion
#endregion
// Save the Excel document in the current HTTP response stream
string outFileName = workbookFormat == ExcelWorkbookFormat.Xls_2003 ? "DataValidation.xls" : "DataValidation.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
If radioXlsFormat.Checked Then
workbookFormat = ExcelWorkbookFormat.Xls_2003
Else
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 = "Data validation sample"
workbook.DocumentProperties.Comments =
"Add data validation 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 = 10
textMessageStyle.Font.Bold = True
textMessageStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
textMessageStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right
textMessageStyle.Fill.FillType = ExcelCellFillType.SolidFill
textMessageStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(204, 255, 204)
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 style used for data validation ranges
Dim dataValidationStyle As ExcelCellStyle = workbook.Styles.AddStyle("DataValidationStyle")
dataValidationStyle.Font.Size = 10
dataValidationStyle.Font.Bold = True
dataValidationStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
dataValidationStyle.Fill.FillType = ExcelCellFillType.SolidFill
dataValidationStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(153, 204, 0)
dataValidationStyle.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Thin
dataValidationStyle.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Thin
dataValidationStyle.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Thin
dataValidationStyle.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Thin
' get the first worksheet in the workbook
Dim worksheet As ExcelWorksheet = workbook.Worksheets.Item(0)
' set the default worksheet name
worksheet.Name = "Data Validation Demo"
' WORKSHEET PAGE SETUP
' set worksheet paper size and orientation, margins, header and footer
worksheet.PageSetup.PaperSize = ExcelPagePaperSize.PaperA4
worksheet.PageSetup.Orientation = ExcelPageOrientation.Portrait
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 = "Adding Data
Validation to an Excel Worksheet"
' 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
' Add Data Validation
' Validate data from a list
worksheet.Item("A5:E5").Merge()
worksheet.Item("A5:E5").Style = textMessageStyle
worksheet.Item("A5:E5").Value = "Select a
value from the list:"
' set the range to be validated
worksheet.Item("G5").Style = dataValidationStyle
worksheet.Item("G5").ColumnWidthInChars = 25
worksheet.Item("G5").AddComment("Click this
cell to select a value from list.")
Dim listValidator As ExcelDataValidator = worksheet.Item("G5").DataValidator
listValidator.AllowedDataType = ExcelDataValidatorDataType.List
listValidator.AllowedValues = New String() {"HTML to PDF Converter", "PDF Merge",
"PDF Security", "Excel Library for .NET"}
listValidator.InputMessageText = "Select a value from the list"
listValidator.ShowInputMessage = True
' Validate a whole number between 0 and 10
worksheet.Item("A7:E7").Merge()
worksheet.Item("A7:E7").Style = textMessageStyle
worksheet.Item("A7:E7").Value = "Enter a
whole number between 0 and 10 :"
' set the range to be validated
worksheet.Item("G7").Style = dataValidationStyle
worksheet.Item("G7").ColumnWidthInChars = 25
worksheet.Item("G7").AddComment("Click this
cell to enter a whole number.")
' Data Validation for Numbers
Dim wholeNumberValidator As ExcelDataValidator = worksheet.Item("G7").DataValidator
wholeNumberValidator.AllowedDataType = ExcelDataValidatorDataType.WholeNumber
wholeNumberValidator.Operator = ExcelDataValidatorOperator.Between
wholeNumberValidator.Value1 = 0
wholeNumberValidator.Value2 = 10
wholeNumberValidator.ErrorAlertText = "A number between 0 to 10 is
expected"
wholeNumberValidator.ShowErrorAlert = True
wholeNumberValidator.ErrorAlertTitle = "Whole Number Validation Error"
wholeNumberValidator.InputMessageText = "Enter a whole number between
0 and 10"
wholeNumberValidator.ShowInputMessage = True
' Validate a date between 01/01/2000 and 12/31/2009
worksheet.Item("A9:E9").Merge()
worksheet.Item("A9:E9").Style = textMessageStyle
worksheet.Item("A9:E9").Value = "Enter a
date between 01/01/2000 and 12/31/2009 :"
' set the range to be validated
worksheet.Item("G9").Style = dataValidationStyle
worksheet.Item("G9").ColumnWidthInChars = 25
worksheet.Item("G9").Style.Number.NumberFormatString = "m/d/yyyy"
worksheet.Item("G9").Value = New DateTime(2008, 12, 15) ' default value
worksheet.Item("G9").AddComment("Double-Click
this cell to enter a date in local format.")
Dim dateValidator As ExcelDataValidator = worksheet.Item("G9").DataValidator
dateValidator.AllowedDataType = ExcelDataValidatorDataType.Date
dateValidator.Operator = ExcelDataValidatorOperator.Between
dateValidator.Value1 = New DateTime(2000, 1, 1)
dateValidator.Value2 = New DateTime(2009, 12, 31)
dateValidator.ErrorAlertText = "A date between 01/01/2000 and 12/31/2009
is expected"
dateValidator.ShowErrorAlert = True
dateValidator.ErrorAlertTitle = "Date Validation Error"
dateValidator.InputMessageText = "Enter a date between 01/01/2000
and 12/31/2009"
dateValidator.ShowInputMessage = True
' Validate the length of a text
worksheet.Item("A11:E11").Merge()
worksheet.Item("A11:E11").Style = textMessageStyle
worksheet.Item("A11:E11").Value = "Enter
a text with length between 2 and 5 chars:"
' set the range to be validated
worksheet.Item("G11").Style = dataValidationStyle
worksheet.Item("G11").ColumnWidthInChars = 25
worksheet.Item("G11").AddComment("Click this
cell to enter a text.")
Dim textLengthValidator As ExcelDataValidator = worksheet.Item("G11").DataValidator
textLengthValidator.AllowedDataType = ExcelDataValidatorDataType.TextLength
textLengthValidator.Operator = ExcelDataValidatorOperator.Between
textLengthValidator.Value1 = 2
textLengthValidator.Value2 = 5
textLengthValidator.ErrorAlertTitle = "Text Length Validation Error"
textLengthValidator.ErrorAlertText = "A text with length between 2
and 5 chars is expected"
textLengthValidator.ShowErrorAlert = True
textLengthValidator.InputMessageText = "Enter a text with length between
2 and 5 characters"
textLengthValidator.ShowInputMessage = True
' SAVE THE WORKBOOK
' Save the Excel document in the current HTTP response stream
Dim outFileName As String
If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
outFileName = "DataValidation.xls"
Else
outFileName = "DataValidation.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
|