首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在API端转换excel?

如何在API端转换excel?
EN

Stack Overflow用户
提问于 2019-12-07 12:38:30
回答 1查看 40关注 0票数 1

作为一个问题,如何在API中转换excel并将其作为文件传回服务?目前我已经能够从数据库中获取数据,但在从数据库中获取数据后,我不确定如何在将数据发送回服务之前将数据转换为excel。我需要在API中转换它,而不是在角度组件端后,从api获得完整的数据。

代码语言:javascript
复制
    [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();
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2019-12-07 13:25:14

下面的代码对我很有效。

请查找更多的elobarations here

代码语言:javascript
复制
#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");
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59223048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档