如何将参数从c#发送到excel并将数据保存在那里
我面临两个问题如何将参数从c#发送到excel并将数据保存在那里,c#,visual-studio-2008,C#,Visual Studio 2008,我面临两个问题 我有一个数据集,需要将数据从数据集发送到 excel一旦将数据转储到该位置 我需要更改列标题,使其加粗 2: 在报告标题上方,我们应该传递一个参数,该参数将是c#中的名称(员工详细信息),我们需要作为参数传递给它。 它可以改变我们传递给它的任何参数 例: 报告名称:员工详细信息 Name EmpID city Arun 11 bangalore Kiran 56 chennai Rahul
- 我有一个数据集,需要将数据从数据集发送到 excel一旦将数据转储到该位置
- 我需要更改列标题,使其加粗
Name EmpID city
Arun 11 bangalore
Kiran 56 chennai
Rahul 23 pune
以下内容应该可以工作,但我没有测试它。感谢您编写以下大部分代码
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();
}
使用Excel=Microsoft.Office.Interop.Excel;
运用系统反思;
私有void ExportToExcel(数据表、字符串报告名、字符串文件名)
{
Excel.applicationoxl;
Excel.oWB工作手册;
Excel.oSheet工作表;
Excel.Range橙色;
//启动Excel并获取应用程序对象。
oXL=新的Excel.Application();
//设置一些属性
可见=真;
oXL.DisplayAlerts=false;
//获取新工作簿。
oWB=oXL.Workbooks.Add(缺少.Value);
//获取活动工作表
oSheet=(Excel.Worksheet)oWB.ActiveSheet;
oSheet.Name=“报告”;
int rowCount=3;
foreach(Table.Rows中的数据行dr)
{
for(int i=1;i
您的问题有哪些?你有密码吗?