csharp Excel文件构造函数C#NET Core
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Excel文件构造函数C#NET Core相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace Barcelo.API.FileGenerator
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSwaggerGen(swagger =>
{
swagger.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "File Generator" });
swagger.DescribeAllEnumsAsStrings();
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "File Generator");
});
app.UseMvc();
}
}
}
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Microsoft.AspNetCore.Mvc;
namespace Barcelo.API.FileGenerator.Controllers
{
public class ExcelController : Controller
{
private const string FILENAME = "document";
private const string FILEEXTENSION = "xlsx";
[HttpPost]
[Route("api/excel")]
public async System.Threading.Tasks.Task<IActionResult> PostAsync([FromBody]Models.ExcelStructure json)
{
// Params
string tempFileName = Guid.NewGuid().ToString();
string path = String.Format("{0}{1}.{2}", Path.GetTempPath(), tempFileName, FILEEXTENSION);
// Build document
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet1 = workbook.CreateSheet("Sheet1");
IRow rowHeader = sheet1.CreateRow(0);
IFont font = (XSSFFont)workbook.CreateFont();
font.IsBold = true;
ICellStyle style = (XSSFCellStyle)workbook.CreateCellStyle();
style.SetFont(font);
for (int i = 0; i < json.header.Count; i++)
{
ICell cell = rowHeader.CreateCell(i);
cell.CellStyle = style;
cell.SetCellValue(json.header[i]);
}
for (int i = 0; i < json.body.Count; i++)
{
IRow row = sheet1.CreateRow(1 + i);
for (int j = 0; j < json.body[i].Length; j++)
{
ICell cell = row.CreateCell(j);
cell.SetCellValue(json.body[i][j].ToString());
}
}
FileStream fs = System.IO.File.Create(path);
workbook.Write(fs);
// Return
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
MemoryStream ms = new MemoryStream();
using (fs = System.IO.File.OpenRead(path))
{
await fs.CopyToAsync(ms);
ms.Position = 0;
}
fs.Close();
System.IO.File.Delete(path);
return File(ms, "application/octet-stream", $"{FILENAME}.{FILEEXTENSION}");
}
}
}
using System;
using System.Collections.Generic;
namespace Barcelo.API.FileGenerator.Models
{
public class ExcelStructure
{
public List<String> header { get; set; }
public List<string[]> body { get; set; }
}
}
以上是关于csharp Excel文件构造函数C#NET Core的主要内容,如果未能解决你的问题,请参考以下文章
csharp GetColumnLetterFromInt c#Excel
csharp excel报告#spreadsheet #devexpress#c#
csharp 例如-CSHARP-GroupDocs.AssemblyExamples-GenerateReport-GenerateBulletedListFromJsoninOpenExcel.c