Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用类库(.NET Framework)在C#中创建excel文件?_C#_Excel - Fatal编程技术网

如何使用类库(.NET Framework)在C#中创建excel文件?

如何使用类库(.NET Framework)在C#中创建excel文件?,c#,excel,C#,Excel,这是我第一次来这里,我对c#不是很有经验。 为了练习,我想为UiPath创建一个自定义活动,所以我使用类库(.NET Framework),我的自定义操作基于创建一个工作簿excel并保存它 我试过了,但没有成功,我真的不知道该怎么办 该文件是通用文件,我在其中添加: public InArgument<string> NameFile { get; set; } public InArgument<string> PathWorkbookInput { get; set

这是我第一次来这里,我对c#不是很有经验。 为了练习,我想为UiPath创建一个自定义活动,所以我使用类库(.NET Framework),我的自定义操作基于创建一个工作簿excel并保存它

我试过了,但没有成功,我真的不知道该怎么办

该文件是通用文件,我在其中添加:

public InArgument<string> NameFile { get; set; }
public InArgument<string> PathWorkbookInput { get; set; }
publicinargument名称文件{get;set;}
公共InArgument PathWorkbookInput{get;set;}

我需要一个由使用者决定名称和路径的简单文件。

首先,我必须指出,如果您是第一次尝试使用c#,那么从类库开始不是一个好的选择,因为您将无法单独运行该类,并且需要一个调用类库.dll文件的.exe文件。 尝试从Windows窗体应用程序甚至WPF窗体开始

第二,应用程序中的数据存储在哪里?SQL数据库?阵列?名单?还是字典

第三,为了让用户选择要存储的Excel文件的名称和位置,您需要创建一个“FileSaveDialoge”实例,以从用户处获取路径和文件名,然后您可以对该路径执行任何操作

试用

“Microsoft Office Interop Excel”NuGet软件包

并使用以下代码段处理此软件包:

public void CreateExcel()
{

Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
            // creating new WorkBook within Excel application  
            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
            // creating new Excelsheet in workbook  
            Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
            // see the excel sheet behind the program  
            app.Visible = true;
            // get the reference of first sheet. By default its name is Sheet1.  
            // store its reference to worksheet  
            worksheet = workbook.Sheets["Sheet1"];
            worksheet = workbook.ActiveSheet;
            // changing the name of active sheet  
            worksheet.Name = "new sheet name";

            // storing Each row and column value to excel sheet  //for example from a grid view
            for (int i = 0; i < GridView.Rows.Count - 1; i++)
            {
                for (int j = 0; j < GridView.Columns.Count; j++)
                {
                    worksheet.Cells[i + 2, j + 1] = GridView.Rows[i].Cells[j].Value.ToString();
                }
            }
            // save the application  
            string path;
            SelectSavePath.ShowDialog();
            path = SelectSavePath.SelectedPath;
            if(path!=string.Empty)
            workbook.SaveAs(path+FileName+".xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            // Exit from the application  
            app.Quit();
}
public void CreateExcel()
{
Microsoft.Office.Interop.Excel.\u应用程序app=新的Microsoft.Office.Interop.Excel.Application();
//在Excel应用程序中创建新工作簿
Microsoft.Office.Interop.Excel.\u工作簿=app.Workbooks.Add(Type.Missing);
//在工作簿中创建新的Excel工作表
Microsoft.Office.Interop.Excel.\u工作表=null;
//请参见程序后面的excel表
app.Visible=true;
//获取第一张图纸的引用。默认情况下,其名称为Sheet1。
//将其引用存储到工作表
工作表=工作簿。工作表[“Sheet1”];
工作表=工作簿.ActiveSheet;
//更改活动图纸的名称
工作表.Name=“新工作表名称”;
//将每行和每列值存储到excel工作表//例如从网格视图
对于(int i=0;i
谢谢你的回答,这就是我要做的
我的数据存储在数组中

谢谢

公共类WriteExecutiveSummary:CodeActivity {

公共类WriteExecutiveSummary:CodeActivity
{  
[类别(“输入”)]
[必需参数]
公共InArgument头{get;set;}
[类别(“输入”)]
[必需参数]
公共InArgument PathWorkbookInput{get;set;}
[类别(“输入”)]
[必需参数]
公共InArgument文件{get;set;}
[类别(“输入”)]
[必需参数]
公共InArgument命名表{get;set;}
[类别(“产出”)]
公共输出参数输出{get;set;}
受保护的覆盖无效执行(CodeActivityContext上下文)
{
Excel.Application xlApp=new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook numovo=xlApp.Workbooks.Add();
Excel.Workbook xlWorkBook=xlApp.Workbooks.Add(PathWorkbookInput.Get(context));
var nomeFile=nomeFile.Get(上下文);
var nomeSheet=nomeSheet.Get(上下文);
SaveAs(System.IO.Directory.GetCurrentDirectory()+“\\”+NomeFile+NomeSheet,Excel.XlFileFormat.xlOpenXMLWorkbook);
[类别(“输入”)]
[必需参数]
公共InArgument头{get;set;}
[类别(“输入”)]
[必需参数]
公共InArgument PathWorkbookInput{get;set;}
[类别(“输入”)]
[必需参数]
公共InArgument文件{get;set;}
[类别(“输入”)]
[必需参数]
公共InArgument命名表{get;set;}
[类别(“产出”)]
公共输出参数输出{get;set;}
受保护的覆盖无效执行(CodeActivityContext上下文)
{
Excel.Application xlApp=new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook numovo=xlApp.Workbooks.Add();
Excel.Workbook xlWorkBook=xlApp.Workbooks.Add(PathWorkbookInput.Get(context));
var nomeFile=nomeFile.Get(上下文);
var nomeSheet=nomeSheet.Get(上下文);
}

 public class WriteExecutiveSummary : CodeActivity 
    {  
        [Category("Input")]
        [RequiredArgument]
        public InArgument<string[]> Header { get; set; }

        [Category("Input")]
        [RequiredArgument]
        public InArgument<string> PathWorkbookInput { get; set; }

        [Category("Input")]
        [RequiredArgument]
        public InArgument<string> NomeFile { get; set; }

        [Category ("Input")]
        [RequiredArgument]
        public InArgument<string> NomeSheet { get; set; }

        [Category("Output")]
        public OutArgument<string> Output { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Excel.Workbook Nuovo = xlApp.Workbooks.Add();
            Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(PathWorkbookInput.Get(context));

            var nomeFile  = NomeFile.Get(context);
            var nomeSheet = NomeSheet.Get(context);

            Nuovo.SaveAs(System.IO.Directory.GetCurrentDirectory() + "\\" + NomeFile + NomeSheet, Excel.XlFileFormat.xlOpenXMLWorkbook);
    [Category("Input")]
    [RequiredArgument]
    public InArgument<string[]> Header { get; set; }

    [Category("Input")]
    [RequiredArgument]
    public InArgument<string> PathWorkbookInput { get; set; }

    [Category("Input")]
    [RequiredArgument]
    public InArgument<string> NomeFile { get; set; }

    [Category ("Input")]
    [RequiredArgument]
    public InArgument<string> NomeSheet { get; set; }

    [Category("Output")]
    public OutArgument<string> Output { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
    Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
    Excel.Workbook Nuovo = xlApp.Workbooks.Add();
    Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(PathWorkbookInput.Get(context));

    var nomeFile  = NomeFile.Get(context);
    var nomeSheet = NomeSheet.Get(context);