[英]How create file excel in C# with Class Library (.NET Framework)?
這是我第一次來這里,我對C#經驗不足。 因此,為了練習,我想為UiPath創建一個自定義活動,因此我使用類庫(.NET Framework),我的自定義操作基於創建一個工作簿excel並將其保存。
我嘗試了但沒用,我真的不知道該怎么辦。
該文件是通用文件,我在其中添加:
public InArgument<string> NameFile { get; set; }
public InArgument<string> PathWorkbookInput { get; set; }
我需要一個簡單的文件,由使用者決定名稱和路徑。
首先,我不得不提一下,如果這是您第一次嘗試使用c#,則類庫不是一個很好的開始,因為您將無法單獨運行該類,並且需要一個.exe文件來調用類庫.dll文件。 嘗試從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();
}
謝謝您的回答,這就是我的工作我的數據存儲在數組中
謝謝
公共類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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.