在C# Web API中实现数据导出功能,通常有以下几种常见的格式:CSV、Excel、PDF等。下面我将分别介绍如何使用C# Web API实现这些格式的数据导出。
首先,创建一个CSV文件并添加以下代码:
public class CsvExportHelper
{
public static string ExportToCsv<T>(IEnumerable<T> data)
{
using (var memoryStream = new MemoryStream())
{
using (var writer = new StreamWriter(memoryStream))
{
var properties = typeof(T).GetProperties();
// Write header
writer.WriteLine(string.Join(",", properties.Select(p => p.Name)));
// Write data
foreach (var item in data)
{
writer.WriteLine(string.Join(",", properties.Select(p => p.GetValue(item, null))));
}
}
memoryStream.Position = 0;
return new StreamReader(memoryStream).ReadToEnd();
}
}
}
接下来,在你的API控制器中调用这个方法:
[HttpGet("export/csv")]
public async Task<IActionResult> ExportCsv()
{
var data = await _yourDataService.GetDataAsync();
var csvContent = CsvExportHelper.ExportToCsv(data);
return File(Encoding.UTF8.GetBytes(csvContent), "text/csv", "data.csv");
}
要实现Excel格式的导出,可以使用第三方库如EPPlus或NPOI。这里以EPPlus为例:
首先,安装EPPlus库:
Install-Package EPPlus
然后,创建一个Excel文件并添加以下代码:
public class ExcelExportHelper
{
public static byte[] ExportToExcel<T>(IEnumerable<T> data)
{
var package = new ExcelPackage();
var worksheet = package.Workbook.Worksheets.Add("Data");
var properties = typeof(T).GetProperties();
// Write header
var headerRow = worksheet.Cells["A1"];
headerRow.LoadFromEnumerable(properties.Select(p => p.Name));
// Write data
var rowIndex = 2;
foreach (var item in data)
{
var row = worksheet.Cells[rowIndex, 1];
row.LoadFromEnumerable(properties.Select(p => p.GetValue(item, null)));
rowIndex++;
}
using (var memoryStream = new MemoryStream())
{
package.SaveAs(memoryStream);
memoryStream.Position = 0;
return memoryStream.ToArray();
}
}
}
在你的API控制器中调用这个方法:
[HttpGet("export/excel")]
public async Task<IActionResult> ExportExcel()
{
var data = await _yourDataService.GetDataAsync();
var excelContent = ExcelExportHelper.ExportToExcel(data);
return File(excelContent, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "data.xlsx");
}
要实现PDF格式的导出,可以使用第三方库如iTextSharp或PdfSharp。这里以iTextSharp为例:
首先,安装iTextSharp库:
Install-Package iTextSharp
然后,创建一个PDF文件并添加以下代码:
public class PdfExportHelper
{
public static byte[] ExportToPdf<T>(IEnumerable<T> data)
{
using (var memoryStream = new MemoryStream())
{
var document = new Document();
using (var writer = PdfWriter.GetInstance(document, memoryStream))
{
document.Open();
var properties = typeof(T).GetProperties();
// Write header
var headerFont = new Font(FontFamily.Helvetica, 12, FontStyle.Bold);
var headerText = new Paragraph("Data", headerFont);
document.Add(headerText);
// Write data
var table = new Table(properties.Count());
table.SetCellValue(0, 0, "Property");
table.SetCellValue(0, 1, "Value");
int rowIndex = 1;
foreach (var item in data)
{
for (int i = 0; i < properties.Count(); i++)
{
table.SetCellValue(rowIndex, i, properties[i].GetValue(item, null).ToString());
}
rowIndex++;
}
document.Add(table);
}
document.Close();
memoryStream.Position = 0;
return memoryStream.ToArray();
}
}
}
在你的API控制器中调用这个方法:
[HttpGet("export/pdf")]
public async Task<IActionResult> ExportPdf()
{
var data = await _yourDataService.GetDataAsync();
var pdfContent = PdfExportHelper.ExportToPdf(data);
return File(pdfContent, "application/pdf", "data.pdf");
}
这样,你就可以在C# Web API中实现CSV、Excel和PDF格式的数据导出了。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。