Skip to content

Commit

Permalink
Merge pull request #14 from zaheertariq/master
Browse files Browse the repository at this point in the history
Export GridView to Excel Initial commit
  • Loading branch information
aspose-cells-gists committed Sep 18, 2015
2 parents d823d15 + dc9b789 commit f2c6a4e
Show file tree
Hide file tree
Showing 28 changed files with 1,285 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Plugins/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ This section contains the following Platforms
* [Aspose.Cells Vs OpenXML Spreadsheets](Aspose.Cells Vs OpenXML Spreadsheets)
* [Aspose.Cells Vs VSTO Spreadsheets](Aspose.Cells Vs VSTO Spreadsheets)
* [Microsoft Dynamics CRM](Dynamics CRM)
* [Export Members to Excel](Aspose.UmbracoMemberExportToExcel)
* [NPOI - .NET version of POI Java project](NPOI)
* [Umbraco - Export Members to Excel](Aspose.UmbracoMemberExportToExcel)
* [NPOI - .NET version of POI Java project](NPOI)
* [Visual Studio](VisualStudio)
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D773C32F-6001-470C-9436-B07E11FB5776}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Aspose.Excel.GridViewExport</RootNamespace>
<AssemblyName>Aspose.Excel.GridViewExport</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Aspose.Cells">
<HintPath>..\Resources\Aspose.Cells.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Web" />
<Reference Include="System.XML" />
</ItemGroup>
<ItemGroup>
<Compile Include="ExportableGridView.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="icon.bmp" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
using System;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Configuration;
using Aspose.Cells;
using System.Text;
using System.Drawing;

namespace Aspose.Excel.GridViewExport
{
public enum ExcelOutputFormat
{
Xlsx, Xlsb, Xls, Txt, Csv, Ods
}

[ToolboxBitmap(typeof(ExportGridViewToExcel), "icon.bmp")]
public class ExportGridViewToExcel : GridView, INamingContainer, IPostBackDataHandler
{
Button ExcelExportButton;

/// <summary>
/// Css Class that is applied to the outer div of the export button. To apply css on button you can use .yourClass input { }
/// </summary>
public string ExportButtonCssClass { get; set; }

/// <summary>
/// Heading that is used only in the exported output Excel file.
/// </summary>
public string ExportFileHeading { get; set; }

/// <summary>
/// If you have paging enabled then the default output is current page. To export all pages set this datasource to all rows you want to export to Excel document
/// </summary>
public object ExportDataSource
{
get { return (object)ViewState["Aspose_ExportDataSource"]; }
set { ViewState["Aspose_ExportDataSource"] = value; }
}

/// <summary>
/// Local output path e.g. "c:\\temp" Disk path on server where a copy of the export is automatically saved. Application must have write access to this path.
/// </summary>
public string ExportOutputPathOnServer { get; set; }

/// <summary>
/// Output format of the exported document. Supported formats are Xlsx, Xlsb, Xls, Txt, Csv, Ods
/// </summary>
public ExcelOutputFormat ExcelOutputFormat { get; set; }

/// <summary>
/// Export button text
/// </summary>
public string ExportButtonText { get; set; }

/// <summary>
/// Path to Aspose.Cells license file e.g. c:\\Aspose.Cells.lic
/// </summary>
public string LicenseFilePath { get; set; }

protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
{
var rowCount = base.CreateChildControls(dataSource, dataBinding);
if (ExcelExportButton == null)
CreateExportButton();
Controls.Add(ExcelExportButton);
return rowCount;
}

public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
return false;
}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateExportButton();
}

private void CreateExportButton()
{
ExcelExportButton = new Button();
ExcelExportButton.Text = string.IsNullOrEmpty(ExportButtonText) ? "Export to Excel" : ExportButtonText;
ExcelExportButton.ID = "__aspose_export_to_excel_gridview";
ExcelExportButton.Click += new EventHandler(ExportButton_Click);
}

public void RaisePostDataChangedEvent()
{
}

private String CalculateWidth()
{
string strWidth = "auto";
if (!this.Width.IsEmpty)
{
strWidth = String.Format("{0}{1}", this.Width.Value, ((this.Width.Type == UnitType.Percentage) ? "%" : "px"));
}
return strWidth;
}

protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("<div style='width:" + CalculateWidth() + "'>");
writer.Write("<div class='" + ExportButtonCssClass + "'>");
ExcelExportButton.RenderControl(writer);
ExcelExportButton.Visible = false;
writer.Write("</div>");
writer.Write("<div>");
base.RenderContents(writer);
writer.Write("</div></div>");
}

protected void ExportButton_Click(object sender, EventArgs e)
{
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);

if (ExportDataSource != null)
{
this.AllowPaging = false;
this.DataSource = ExportDataSource;
this.DataBind();
}

this.RenderBeginTag(hw);
this.HeaderRow.RenderControl(hw);
foreach (GridViewRow row in this.Rows)
{
row.RenderControl(hw);
}
this.FooterRow.RenderControl(hw);
this.RenderEndTag(hw);

string heading = string.IsNullOrEmpty(ExportFileHeading) ? string.Empty : ExportFileHeading;

string pageSource = sw.ToString();

// Check for license and apply if exists
if (File.Exists(LicenseFilePath))
{
License license = new License();
license.SetLicense(LicenseFilePath);
}

using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(pageSource)))
{
var loadOptions = new LoadOptions(LoadFormat.Html)
{
ConvertNumericData = false
};

Workbook workbook = new Workbook(stream, loadOptions);

Worksheet worksheet = workbook.Worksheets[0];

worksheet.AutoFitColumns();
worksheet.AutoFitRows();

Aspose.Cells.Cells cells = worksheet.Cells;
Range range = worksheet.Cells.MaxDisplayRange;
int tcols = range.ColumnCount;
int trows = range.RowCount;

for (int i = 0; i < trows; i++)
{
for (int j = 0; j < tcols; j++)
{
if (cells[i, j].Type != CellValueType.IsNull)
{
Aspose.Cells.Style style = cells[i, j].GetStyle();

if (i == 0)
{
style.Font.IsBold = true;
}

style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.TopBorder].Color = Color.Black;
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.BottomBorder].Color = Color.Black;
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.LeftBorder].Color = Color.Black;
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.RightBorder].Color = Color.Black;
cells[i, j].SetStyle(style);
}
}
}

worksheet.Cells.InsertColumn(0);
worksheet.Cells.InsertRow(0);

worksheet.Cells["B1"].HtmlString = heading;
worksheet.Cells.InsertRow(0);
worksheet.Cells.InsertRow(2);

Aspose.Cells.Style style2 = worksheet.Cells["B2"].GetStyle();
style2.Font.Size = 20;
style2.Borders.SetStyle(CellBorderType.None);
worksheet.Cells["B2"].SetStyle(style2);

string extension = ExcelOutputFormat.ToString().ToLower();

if (string.IsNullOrEmpty(extension)) extension = "xls";
string fileNameWithExtension = System.Guid.NewGuid() + "." + extension;

if (!string.IsNullOrEmpty(ExportOutputPathOnServer) && Directory.Exists(ExportOutputPathOnServer))
{
try
{
workbook.Save(ExportOutputPathOnServer + "\\" + fileNameWithExtension);
}
catch (Exception) { }
}

workbook.Save(HttpContext.Current.Response, fileNameWithExtension, ContentDisposition.Inline, GetSaveFormat(ExcelOutputFormat.ToString()));
HttpContext.Current.Response.End();
}
}

private XlsSaveOptions GetSaveFormat(string format)
{
XlsSaveOptions saveOption = new XlsSaveOptions(SaveFormat.Xlsx);

switch (format.ToLower())
{
case "xlsx":
saveOption = new XlsSaveOptions(SaveFormat.Xlsx); break;
case "xlsb":
saveOption = new XlsSaveOptions(SaveFormat.Xlsb); break;
case "xls":
saveOption = new XlsSaveOptions(SaveFormat.Excel97To2003); break;
case "txt":
saveOption = new XlsSaveOptions(SaveFormat.TabDelimited); break;
case "csv":
saveOption = new XlsSaveOptions(SaveFormat.CSV); break;
case "ods":
saveOption = new XlsSaveOptions(SaveFormat.ODS); break;
default:
saveOption = new XlsSaveOptions(SaveFormat.Excel97To2003); break;
}

return saveOption;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Aspose.Excel.GridViewExport")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Aspose.Excel.GridViewExport")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("6ca64ee5-3dc2-46c4-a72f-d9edfecacec6")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Binary file not shown.
Loading

0 comments on commit f2c6a4e

Please sign in to comment.