您好,登錄后才能下訂單哦!
在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。