this is my first time here, I am not very experienced with c#. So for practice, I would like to create a Custom Activity for UiPath, so I use Class Library (.NET Framework), my custom action is based to create one workbook excel and save it.
I tried but doesn't work, I really not knowing what to do.
The file is generic, where I add :
public InArgument<string> NameFile { get; set; }
public InArgument<string> PathWorkbookInput { get; set; }
I need a simple file where the consumer decides the name and the path.
first, i have to mention that if it's your first try with c#, a class library is not a good choice to start with, as you will not be able to run the class alone and you are going to need a .exe file that calls the class library .dll file. try starting with a Windows form application or even a WPF form
second, where is your data stored in the application? SQL DB? an Array? a list? or a Dictionary?
third, for letting the user choose a name and location for Excel file to be stored, you will need to create an instance of "FileSaveDialoge" to get the path and file name from the user, then you are able to do whatever you want with the path
try using
"Microsoft Office Interop Excel" NuGet package
and use the snippet below to work with this package:
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();
}
Thank you for answer, this is what i do My data stored in the array
Thanks
public class WriteExecutiveSummary : CodeActivity {
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.