Skip to content

Commit 8a6c73d

Browse files
author
JanKallman
committed
New sample 1, 2 and 5. New Web sample project.Fix a few bugs. This will be the last check in before next version, I hope, so please send me a message if you find any bugs, strange behaviors or misspellings in this code.
--HG-- extra : convert_revision : svn%3Ac031521d-7fdb-604e-9ef1-0138de3cd895/trunk%4028
1 parent 5ec4302 commit 8a6c73d

26 files changed

+853
-216
lines changed

EPPlus.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1313
LocalTestRun.testrunconfig = LocalTestRun.testrunconfig
1414
EndProjectSection
1515
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EPPlusWebSample", "EPPlusWebSample\EPPlusWebSample.csproj", "{1BF30A52-6149-432D-82F6-725250E5C662}"
17+
EndProject
1618
Global
1719
GlobalSection(TestCaseManagementSettings) = postSolution
1820
CategoryFile = ExcelPackage.vsmdi
@@ -34,6 +36,10 @@ Global
3436
{E7BCEDE0-EADD-437B-86D5-49D192216948}.Debug|Any CPU.Build.0 = Debug|Any CPU
3537
{E7BCEDE0-EADD-437B-86D5-49D192216948}.Release|Any CPU.ActiveCfg = Release|Any CPU
3638
{E7BCEDE0-EADD-437B-86D5-49D192216948}.Release|Any CPU.Build.0 = Release|Any CPU
39+
{1BF30A52-6149-432D-82F6-725250E5C662}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
40+
{1BF30A52-6149-432D-82F6-725250E5C662}.Debug|Any CPU.Build.0 = Debug|Any CPU
41+
{1BF30A52-6149-432D-82F6-725250E5C662}.Release|Any CPU.ActiveCfg = Release|Any CPU
42+
{1BF30A52-6149-432D-82F6-725250E5C662}.Release|Any CPU.Build.0 = Release|Any CPU
3743
EndGlobalSection
3844
GlobalSection(SolutionProperties) = preSolution
3945
HideSolutionNode = FALSE

EPPlusWebSample/Default.aspx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="EPPlusWebSample._Default" %>
2+
3+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4+
5+
<html xmlns="http://www.w3.org/1999/xhtml" >
6+
<head runat="server">
7+
<title>EPPlus Websample</title>
8+
<style type="text/css">
9+
body
10+
{
11+
BACKGROUND-COLOR:#FFFFFF;
12+
COLOR: #111111;
13+
FONT-FAMILY: Arial;
14+
font-size large;
15+
margin-top: 1px
16+
}
17+
table
18+
{
19+
FONT-FAMILY: Arial;
20+
FONT-SIZE: medium
21+
}
22+
A:link {color:#003399;}
23+
A:visited {color:#003399;}
24+
A:hover{color:#CC6699}
25+
26+
</style>
27+
</head>
28+
<body>
29+
<form id="form1" runat="server">
30+
<h1>EPPlus Web samples</h1>
31+
<h3>The web sample project shows a few different ways to send a workbook to the client. </h3>
32+
<table>
33+
<tr>
34+
<td>
35+
<asp:HyperLink ID="sample1" runat="server" NavigateUrl="~/GetSample.aspx?Sample=1">Sample 1</asp:HyperLink>
36+
</td>
37+
<td>
38+
This sample demonstrates how to output a spreadsheet using the SaveAs(Reponse.OutputSteam) method.
39+
</td>
40+
</tr>
41+
<tr>
42+
<td>
43+
<asp:HyperLink ID="sample2" runat="server" NavigateUrl="~/GetSample.aspx?Sample=2">Sample 2</asp:HyperLink>
44+
</td>
45+
<td>
46+
This sample demonstrates how to output a spreadsheet using the Response.BinaryWrite(pck.GetAsByteArray()).
47+
</td>
48+
</tr>
49+
<tr>
50+
<td>
51+
<asp:HyperLink ID="sample3" runat="server" NavigateUrl="~/GetSample.aspx?Sample=3">Sample 3</asp:HyperLink>
52+
</td>
53+
<td>
54+
This sample demonstrates how to use a template stored in the Application cashe.
55+
</td>
56+
</tr>
57+
</table>
58+
</form>
59+
</body>
60+
</html>

EPPlusWebSample/Default.aspx.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
using System.Web.UI;
6+
using System.Web.UI.WebControls;
7+
using OfficeOpenXml;
8+
9+
namespace EPPlusWebSample
10+
{
11+
public partial class _Default : System.Web.UI.Page
12+
{
13+
protected void Page_Load(object sender, EventArgs e)
14+
{
15+
16+
}
17+
}
18+
}

EPPlusWebSample/Default.aspx.designer.cs

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<PropertyGroup>
3+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
4+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
5+
<ProductVersion>9.0.30729</ProductVersion>
6+
<SchemaVersion>2.0</SchemaVersion>
7+
<ProjectGuid>{1BF30A52-6149-432D-82F6-725250E5C662}</ProjectGuid>
8+
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
9+
<OutputType>Library</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>EPPlusWebSample</RootNamespace>
12+
<AssemblyName>EPPlusWebSample</AssemblyName>
13+
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="System" />
34+
<Reference Include="System.Data" />
35+
<Reference Include="System.Core">
36+
<RequiredTargetFramework>3.5</RequiredTargetFramework>
37+
</Reference>
38+
<Reference Include="System.Data.DataSetExtensions">
39+
<RequiredTargetFramework>3.5</RequiredTargetFramework>
40+
</Reference>
41+
<Reference Include="System.Web.Extensions">
42+
<RequiredTargetFramework>3.5</RequiredTargetFramework>
43+
</Reference>
44+
<Reference Include="System.Xml.Linq">
45+
<RequiredTargetFramework>3.5</RequiredTargetFramework>
46+
</Reference>
47+
<Reference Include="System.Drawing" />
48+
<Reference Include="System.Web" />
49+
<Reference Include="System.Xml" />
50+
<Reference Include="System.Configuration" />
51+
<Reference Include="System.Web.Services" />
52+
<Reference Include="System.EnterpriseServices" />
53+
<Reference Include="System.Web.Mobile" />
54+
</ItemGroup>
55+
<ItemGroup>
56+
<Content Include="Default.aspx" />
57+
<Content Include="GetSample.aspx" />
58+
<Content Include="Web.config" />
59+
</ItemGroup>
60+
<ItemGroup>
61+
<Compile Include="Default.aspx.cs">
62+
<SubType>ASPXCodeBehind</SubType>
63+
<DependentUpon>Default.aspx</DependentUpon>
64+
</Compile>
65+
<Compile Include="Default.aspx.designer.cs">
66+
<DependentUpon>Default.aspx</DependentUpon>
67+
</Compile>
68+
<Compile Include="GetSample.aspx.cs">
69+
<DependentUpon>GetSample.aspx</DependentUpon>
70+
<SubType>ASPXCodeBehind</SubType>
71+
</Compile>
72+
<Compile Include="GetSample.aspx.designer.cs">
73+
<DependentUpon>GetSample.aspx</DependentUpon>
74+
</Compile>
75+
<Compile Include="Properties\AssemblyInfo.cs" />
76+
</ItemGroup>
77+
<ItemGroup>
78+
<ProjectReference Include="..\ExcelPackage\EPPlus.csproj">
79+
<Project>{7B288026-5502-4A39-BF41-77E086F3E4A3}</Project>
80+
<Name>EPPlus</Name>
81+
</ProjectReference>
82+
</ItemGroup>
83+
<ItemGroup>
84+
<Folder Include="App_Data\" />
85+
</ItemGroup>
86+
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
87+
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" />
88+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
89+
Other similar extension points exist, see Microsoft.Common.targets.
90+
<Target Name="BeforeBuild">
91+
</Target>
92+
<Target Name="AfterBuild">
93+
</Target>
94+
-->
95+
<ProjectExtensions>
96+
<VisualStudio>
97+
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
98+
<WebProjectProperties>
99+
<UseIIS>False</UseIIS>
100+
<AutoAssignPort>True</AutoAssignPort>
101+
<DevelopmentServerPort>2529</DevelopmentServerPort>
102+
<DevelopmentServerVPath>/</DevelopmentServerVPath>
103+
<IISUrl>
104+
</IISUrl>
105+
<NTLMAuthentication>False</NTLMAuthentication>
106+
<UseCustomServer>False</UseCustomServer>
107+
<CustomServerUrl>
108+
</CustomServerUrl>
109+
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
110+
</WebProjectProperties>
111+
</FlavorProperties>
112+
</VisualStudio>
113+
</ProjectExtensions>
114+
</Project>

EPPlusWebSample/GetSample.aspx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetSample.aspx.cs" Inherits="EPPlusWebSample.GetSample" %>

EPPlusWebSample/GetSample.aspx.cs

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Web;
5+
using System.Web.UI;
6+
using System.Web.UI.WebControls;
7+
using OfficeOpenXml;
8+
using System.IO;
9+
using OfficeOpenXml.Style;
10+
using System.Drawing;
11+
12+
namespace EPPlusWebSample
13+
{
14+
public partial class GetSample : System.Web.UI.Page
15+
{
16+
protected void Page_Load(object sender, EventArgs e)
17+
{
18+
switch (Request.QueryString["Sample"])
19+
{
20+
case "1":
21+
Sample1();
22+
break;
23+
case "2":
24+
Sample2();
25+
break;
26+
case "3":
27+
Sample3();
28+
break;
29+
default:
30+
Response.Write("<script>javascript:alert('Invalid querystring');</script>");
31+
break;
32+
33+
}
34+
}
35+
36+
/// <summary>
37+
/// Sample 1
38+
/// Demonstrates the SaveAs method
39+
/// </summary>
40+
private void Sample1()
41+
{
42+
ExcelPackage pck = new ExcelPackage();
43+
var ws = pck.Workbook.Worksheets.Add("Sample1");
44+
45+
ws.Cells["A1"].Value = "Sample 1";
46+
ws.Cells["A1"].Style.Font.Bold = true;
47+
var shape = ws.Drawings.AddShape("Shape1", eShapeStyle.Rect);
48+
shape.SetPosition(50, 200);
49+
shape.SetSize(200, 100);
50+
shape.Text = "Sample 1 saves to the Response.OutputStream";
51+
52+
pck.SaveAs(Response.OutputStream);
53+
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
54+
Response.AddHeader("content-disposition", "attachment; filename=Sample1.xlsx");
55+
}
56+
/// <summary>
57+
/// Sample 2
58+
/// Demonstrates the GetAsByteArray method
59+
/// </summary>
60+
private void Sample2()
61+
{
62+
ExcelPackage pck = new ExcelPackage();
63+
var ws = pck.Workbook.Worksheets.Add("Sample2");
64+
65+
ws.Cells["A1"].Value = "Sample 2";
66+
ws.Cells["A1"].Style.Font.Bold = true;
67+
var shape = ws.Drawings.AddShape("Shape1", eShapeStyle.Rect);
68+
shape.SetPosition(50, 200);
69+
shape.SetSize(200, 100);
70+
shape.Text = "Sample 2 outputs the sheet using the Response.BinaryWrite method";
71+
72+
Response.BinaryWrite(pck.GetAsByteArray());
73+
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
74+
Response.AddHeader("content-disposition", "attachment; filename=Sample1.xlsx");
75+
}
76+
/// <summary>
77+
/// Sample 3
78+
/// Uses a cached template
79+
/// </summary>
80+
private void Sample3()
81+
{
82+
if (Application["Sample3Template"] == null) //Check if the template is loaded
83+
{
84+
//Here we create the template.
85+
//As an alternative the template could be loaded from disk or from a resource.
86+
ExcelPackage pckTemplate = new ExcelPackage();
87+
var wsTemplate = pckTemplate.Workbook.Worksheets.Add("Sample3");
88+
89+
wsTemplate.Cells["A1"].Value = "Sample 3";
90+
wsTemplate.Cells["A1"].Style.Font.Bold = true;
91+
var shape = wsTemplate.Drawings.AddShape("Shape1", eShapeStyle.Rect);
92+
shape.SetPosition(50, 200);
93+
shape.SetSize(200, 100);
94+
shape.Text = "Sample 3 uses a template that is stored in the application cashe.";
95+
pckTemplate.Save();
96+
97+
Application["Sample3Template"] = pckTemplate.Stream;
98+
}
99+
//Open the new package with the template stream.
100+
//The template stream is copied to the new stream in the constructor
101+
ExcelPackage pck = new ExcelPackage(new MemoryStream(), Application["Sample3Template"] as Stream);
102+
var ws = pck.Workbook.Worksheets[1];
103+
int row = new Random().Next(10) + 10; //Pick a random row to print the text
104+
ws.Cells[row,1].Value = "We make a small change here, after the template has been loaded...";
105+
ws.Cells[row, 1, row, 5].Style.Fill.PatternType = ExcelFillStyle.Solid;
106+
ws.Cells[row, 1, row, 5].Style.Fill.BackgroundColor.SetColor(Color.LightGoldenrodYellow);
107+
108+
Response.BinaryWrite(pck.GetAsByteArray());
109+
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
110+
Response.AddHeader("content-disposition", "attachment; filename=Sample1.xlsx");
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)