如何将参数从c#发送到excel并在那里保存数据

发布于 2024-12-28 15:59:26 字数 377 浏览 0 评论 0原文

我面临两个问题

  • 我有一个数据集,我需要将数据从数据集发送到 一旦数据转储到该位置,就会显示 Excel。

  • 我需要更改列标题,使其粗体

2: 在报表标题上方,我们应该传递 1 个参数,该参数将是我们需要作为参数传递给它的 C# 中的名称(员工详细信息)。 它可以改变我们传递给它的任何参数。

前任: 报告名称:员工详细信息

  Name      EmpID   city
  Arun        11       bangalore
  Kiran       56       chennai
  Rahul       23       pune

i have 2 issues what i am facing

  • i have an dataset where i need to send the data from dataset to an
    excel once data in dumped in that location.

  • i need to change the column headers make them bold

2:
Above the report headers we should pass 1 parameter that will be the name(Employee details) from c# we need to pass as an parameter to it.
it can change what ever parameter we pass it on.

ex:
Reportname: Employee details

  Name      EmpID   city
  Arun        11       bangalore
  Kiran       56       chennai
  Rahul       23       pune

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走走停停 2025-01-04 15:59:26

以下应该有效,但我没有测试它。感谢您 Deborah Kurata 编写了下面的大部分代码。

using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;

private void ExportToExcel(DataTable Table, string ReportName, string Filename)
{
  Excel.Application oXL;
  Excel.Workbook oWB;
  Excel.Worksheet oSheet;
  Excel.Range oRange;

  // Start Excel and get Application object.
  oXL = new Excel.Application();

  // Set some properties
  oXL.Visible = true;
  oXL.DisplayAlerts = false;

  // Get a new workbook.
  oWB = oXL.Workbooks.Add(Missing.Value);

  // Get the active sheet
  oSheet = (Excel.Worksheet)oWB.ActiveSheet ;
  oSheet.Name = "Report";

  int rowCount = 3;
  foreach (DataRow dr in Table.Rows)
  {
      for (int i = 1; i < Table.Columns.Count+1; i++)
      {
          // Add the header the first time through
          if (rowCount==3)
          {
              oSheet.Cells[1, i] = Table.Columns[i - 1].ColumnName;
              rowCount++;
          }
          oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
      }
      rowCount++;
  }

  // Resize the columns
  oRange = oSheet.get_Range(oSheet.Cells[3, 1],
                oSheet.Cells[rowCount, Table.Columns.Count]);
  oRange.EntireColumn.AutoFit();

  // Set report title *after* we adjust column widths
  oSheet.Cells[1,1] = ReportName;

  // Save the sheet and close
  oSheet = null;
  oRange = null;
  oWB.SaveAs(Filename, Excel.XlFileFormat.xlWorkbookNormal,
      Missing.Value, Missing.Value, Missing.Value, Missing.Value,
      Excel.XlSaveAsAccessMode.xlExclusive,
      Missing.Value, Missing.Value, Missing.Value,
      Missing.Value, Missing.Value);
  oWB.Close(Missing.Value, Missing.Value, Missing.Value);
  oWB = null;
  oXL.Quit();

  // Clean up
  // NOTE: When in release mode, this does the trick
  GC.WaitForPendingFinalizers();
  GC.Collect();
  GC.WaitForPendingFinalizers();
  GC.Collect();
}

The following should work, but I did not test it. Thank you to Deborah Kurata for writing a large part of the code below.

using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;

private void ExportToExcel(DataTable Table, string ReportName, string Filename)
{
  Excel.Application oXL;
  Excel.Workbook oWB;
  Excel.Worksheet oSheet;
  Excel.Range oRange;

  // Start Excel and get Application object.
  oXL = new Excel.Application();

  // Set some properties
  oXL.Visible = true;
  oXL.DisplayAlerts = false;

  // Get a new workbook.
  oWB = oXL.Workbooks.Add(Missing.Value);

  // Get the active sheet
  oSheet = (Excel.Worksheet)oWB.ActiveSheet ;
  oSheet.Name = "Report";

  int rowCount = 3;
  foreach (DataRow dr in Table.Rows)
  {
      for (int i = 1; i < Table.Columns.Count+1; i++)
      {
          // Add the header the first time through
          if (rowCount==3)
          {
              oSheet.Cells[1, i] = Table.Columns[i - 1].ColumnName;
              rowCount++;
          }
          oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
      }
      rowCount++;
  }

  // Resize the columns
  oRange = oSheet.get_Range(oSheet.Cells[3, 1],
                oSheet.Cells[rowCount, Table.Columns.Count]);
  oRange.EntireColumn.AutoFit();

  // Set report title *after* we adjust column widths
  oSheet.Cells[1,1] = ReportName;

  // Save the sheet and close
  oSheet = null;
  oRange = null;
  oWB.SaveAs(Filename, Excel.XlFileFormat.xlWorkbookNormal,
      Missing.Value, Missing.Value, Missing.Value, Missing.Value,
      Excel.XlSaveAsAccessMode.xlExclusive,
      Missing.Value, Missing.Value, Missing.Value,
      Missing.Value, Missing.Value);
  oWB.Close(Missing.Value, Missing.Value, Missing.Value);
  oWB = null;
  oXL.Quit();

  // Clean up
  // NOTE: When in release mode, this does the trick
  GC.WaitForPendingFinalizers();
  GC.Collect();
  GC.WaitForPendingFinalizers();
  GC.Collect();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文