作为一个问题,如何在API中转换excel并将其作为文件传回服务?目前我已经能够从数据库中获取数据,但在从数据库中获取数据后,我不确定如何在将数据发送回服务之前将数据转换为excel。我需要在API中转换它,而不是在角度组件端后,从api获得完整的数据。
[Route("getExcelData")]
[Authenticate]
[HttpPost]
public IHttpActionResult GetExcelData([FromBody]Report obj)
{
try
{
this.Contents.Add("Result", new ReportService().getExcelData(obj.Id, obj.FromDate, obj.ToDate));
return JsonResponse();
}
catch (Exception e)
{
LogManager.Instance.InsertLog(LogLevel.Error, e.Message, e.StackTrace, "ReportController", "ReportController", null, null);
ExceptionManager.Instance.Add(String.Format("GetData Failed: {0}", e.Message), $"{e.StackTrace} Inner Exception: {e.InnerException}");
this.Contents.Add("Result", null);
return JsonResponse();
}
}发布于 2019-12-07 13:25:14
下面的代码对我很有效。
请查找更多的elobarations here。
#region Loading the data to DataGridView
DataSet customersDataSet = new DataSet();
//Read the XML file with data
string inputXmlPath = Path.GetFullPath(@"../../Data/Employees.xml");
customersDataSet.ReadXml(inputXmlPath);
DataTable dataTable = new DataTable();
//Copy the structure and data of the table
dataTable = customersDataSet.Tables[1].Copy();
//Removing unwanted columns
dataTable.Columns.RemoveAt(0);
dataTable.Columns.RemoveAt(10);
this.dataGridView1.DataSource = dataTable;
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.White;
dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightBlue;
dataGridView1.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("Tahoma", 9F, ((System.Drawing.FontStyle)(System.Drawing.FontStyle.Bold)));
dataGridView1.ForeColor = Color.Black;
dataGridView1.BorderStyle = BorderStyle.None;
#endregion
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
//Create a workbook with single worksheet
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];
//Import from DataGridView to worksheet
worksheet.ImportDataGridView(dataGridView1, 1, 1, isImportHeader: true, isImportStyle: true);
worksheet.UsedRange.AutofitColumns();
workbook.SaveAs("Output.xlsx");
}https://stackoverflow.com/questions/59223048
复制相似问题