0
생성 된 .xls 파일을 사용자에게 보내는 내보내기 기능을 만들려고합니다.NPOI는 NancyFx와 함께 .xls 파일을 반환합니다.
요청에 NancyFx를 사용하고 Excel 파일을 만들 때 NPOI를 사용하고 있습니다. 이 코드의 문제점을 알아낼 수 없지만 OK 200 응답을 받았지만 콘텐츠/파일이 반환하지 않습니다.
public class ExportService
{
private HSSFWorkbook HssfWorkbook { get; set; }
public ExportService()
{
HssfWorkbook = new HSSFWorkbook();
}
public Response Export()
{
string fileName = "test2.xls";
var response = new Response();
response.Headers.Add("Content-Disposition", string.Format("attachment;filename={0}", fileName));
InitializeWorkbook();
GenerateData();
response.Contents(WriteToStream());
return response.AsAttachment(fileName, "application/vnd.ms-exce");
}
private MemoryStream WriteToStream()
{
//Write the stream data of workbook to the root directory
MemoryStream file = new MemoryStream();
HssfWorkbook.Write(file);
return file;
}
private void GenerateData()
{
var sheet1 = HssfWorkbook.CreateSheet("Försäljning");
sheet1.CreateRow(0).CreateCell(0).SetCellValue("Detta är ett test");
int x = 1;
for (int i = 1; i <= 15; i++)
{
var row = sheet1.CreateRow(i);
for (int j = 0; j < 15; j++)
{
row.CreateCell(j).SetCellValue(x++);
}
}
}
private void InitializeWorkbook()
{
////create a entry of DocumentSummaryInformation
var documentSummaryInformation = PropertySetFactory.CreateDocumentSummaryInformation();
documentSummaryInformation.Company = "Test Company";
HssfWorkbook.DocumentSummaryInformation = documentSummaryInformation;
////create a entry of SummaryInformation
var summaryInformation = PropertySetFactory.CreateSummaryInformation();
summaryInformation.Subject = "Test Subject";
HssfWorkbook.SummaryInformation = summaryInformation;
}
}