Skip to content

Commit abde15f

Browse files
committed
First commit
1 parent bd15db0 commit abde15f

File tree

6 files changed

+188
-0
lines changed

6 files changed

+188
-0
lines changed

.gitattributes

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Source https://help.github.com/articles/dealing-with-line-endings
2+
# Set default behaviour, in case users don't have core.autocrlf set.
3+
* text=auto
4+
5+
# Explicitly declare text files we want to always be normalized and converted
6+
# to native line endings on checkout.
7+
*.cs text diff=csharp eol=crlf
8+
9+
# Declare files that will always have CRLF line endings on checkout.
10+
*.sln text eol=crlf
11+
12+
core.autocrlf=true

.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
#ignore thumbnails created by windows
3+
Thumbs.db
4+
#Ignore files build by Visual Studio
5+
*.obj
6+
*.exe
7+
*.pdb
8+
*.user
9+
*.aps
10+
*.pch
11+
*.vspscc
12+
*_i.c
13+
*_p.c
14+
*.ncb
15+
*.suo
16+
*.tlb
17+
*.tlh
18+
*.bak
19+
*.cache
20+
*.ilk
21+
*.log
22+
[Bb]in
23+
[Dd]ebug*/
24+
*.lib
25+
*.sbr
26+
obj/
27+
[Rr]elease*/
28+
_ReSharper*/
29+
[Tt]est[Rr]esult*
30+
.vs
31+
packages

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# ClosedXML.Extensions.WebApi
2+
WebApi Extensions for [ClosedXML](https://github.com/ClosedXML/ClosedXML)
3+
4+
## Install via NuGet
5+
6+
To install ClosedXML.Extensions.Mvc, run the following command in the Package Manager Console
7+
8+
```
9+
PM> Install-Package ClosedXML.Extensions.WebApi
10+
```
11+
12+
13+
## Usage
14+
In your WebApi controller define an action that will generate and download your file:
15+
16+
```c#
17+
public class ExcelController : ApiController
18+
{
19+
[HttpGet]
20+
[Route("api/file/{id}")]
21+
public async Task<HttpResponseMessage> DownloadFile(int id)
22+
{
23+
var wb = await BuildExcelFile(id);
24+
return wb.Deliver("excelfile.xlsx");
25+
}
26+
27+
private async Task<XLWorkbook> BuildExcelFile(int id)
28+
{
29+
//Creating the workbook
30+
var t = Task.Run(() =>
31+
{
32+
var wb = new XLWorkbook();
33+
var ws = wb.AddWorksheet("Sheet1");
34+
ws.FirstCell().SetValue(id);
35+
36+
return wb;
37+
});
38+
39+
return await t;
40+
}
41+
}
42+
```
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netcoreapp2.0;net45;net46</TargetFrameworks>
5+
<LangVersion>7.2</LangVersion>
6+
<Version>0.2.0</Version>
7+
<Authors>Francois Botha</Authors>
8+
<Company />
9+
<Product>ClosedXML.Extensions.WebApi</Product>
10+
<Description>WebApi extensions for ClosedXML</Description>
11+
<Copyright>MIT</Copyright>
12+
<PackageLicenseUrl>https://github.com/ClosedXML/ClosedXML.Extensions.WebApi/blob/master/LICENSE</PackageLicenseUrl>
13+
<PackageProjectUrl>https://github.com/ClosedXML.Extensions.WebApi/ClosedXML</PackageProjectUrl>
14+
<RepositoryUrl>https://github.com/ClosedXML/ClosedXML.Extensions.WebApi</RepositoryUrl>
15+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
16+
</PropertyGroup>
17+
18+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
19+
<DefineConstants>$(DefineConstants);_NETSTANDARD_;_NETSTANDARD2_0_</DefineConstants>
20+
</PropertyGroup>
21+
22+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
23+
<DefineConstants>$(DefineConstants);_NETFRAMEWORK_;_NET45_</DefineConstants>
24+
</PropertyGroup>
25+
26+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net46' ">
27+
<DefineConstants>$(DefineConstants);_NETFRAMEWORK_;_NET46_</DefineConstants>
28+
</PropertyGroup>
29+
30+
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
31+
</ItemGroup>
32+
33+
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
34+
<Reference Include="System.Net.Http" />
35+
</ItemGroup>
36+
37+
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
38+
<Reference Include="System.Net.Http" />
39+
</ItemGroup>
40+
41+
<ItemGroup>
42+
<None Include="..\.editorconfig" Link=".editorconfig" />
43+
</ItemGroup>
44+
45+
<ItemGroup>
46+
<PackageReference Include="ClosedXML" Version="0.92.1" />
47+
</ItemGroup>
48+
</Project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using ClosedXML.Excel;
2+
using System.IO;
3+
using System.Net;
4+
using System.Net.Http;
5+
using System.Net.Http.Headers;
6+
7+
namespace ClosedXML.Extensions
8+
{
9+
public static class WebApiExtensions
10+
{
11+
public static HttpResponseMessage Deliver(this XLWorkbook workbook, string fileName, string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
12+
{
13+
var memoryStream = new MemoryStream();
14+
workbook.SaveAs(memoryStream);
15+
memoryStream.Seek(0, SeekOrigin.Begin);
16+
17+
var message = new HttpResponseMessage(HttpStatusCode.OK)
18+
{
19+
Content = new StreamContent(memoryStream)
20+
};
21+
message.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
22+
message.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
23+
{
24+
FileName = fileName
25+
};
26+
27+
return message;
28+
}
29+
}
30+
}

src/ClosedXML.Extensions.WebApi.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27703.2000
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClosedXML.Extensions.WebApi", "ClosedXML.Extensions.Mvc\ClosedXML.Extensions.WebApi.csproj", "{1A7658C5-7686-4478-8193-4D435AB9A9A6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{1A7658C5-7686-4478-8193-4D435AB9A9A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{1A7658C5-7686-4478-8193-4D435AB9A9A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{1A7658C5-7686-4478-8193-4D435AB9A9A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{1A7658C5-7686-4478-8193-4D435AB9A9A6}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {8AC95A78-9554-4890-9482-4CA4DB2B387F}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)