我试图将一些VBA代码转换为C#.我是C#的新手.目前我正在尝试从文件夹中打开Excel文件,如果它不存在则创建它.我正在尝试以下内容.我怎样才能使它工作?
Excel.Application objexcel; Excel.Workbook wbexcel; bool wbexists; Excel.Worksheet objsht; Excel.Range objrange; objexcel = new Excel.Application(); if (Directory("C:\\csharp\\error report1.xls") = "") { wbexcel.NewSheet(); } else { wbexcel.Open("C:\\csharp\\error report1.xls"); objsht = ("sheet1"); } objsht.Activate();
abatishchev.. 75
您需要安装Microsoft Visual Studio Tools for Office.
之后,创建常见的.NET项目,并Microsoft.Office.Interop.Excel
通过"Add Reference .."对话框添加对COM对象的引用.
Application excel = new Application(); Workbook wb = excel.Workbooks.Open(path);
Missing.Value
是不必要的参数替换的特殊反射结构
您需要安装Microsoft Visual Studio Tools for Office.
之后,创建常见的.NET项目,并Microsoft.Office.Interop.Excel
通过"Add Reference .."对话框添加对COM对象的引用.
Application excel = new Application(); Workbook wb = excel.Workbooks.Open(path);
Missing.Value
是不必要的参数替换的特殊反射结构
FileInfo fi = new FileInfo("C:\\test\\report.xlsx"); if(fi.Exists) { System.Diagnostics.Process.Start(@"C:\test\report.xlsx"); } else { //file doesn't exist }
打开Excel文件
System.Diagnostics.Process.Start(@"c:\document.xls");
private void btnChoose2_Click(object sender, EventArgs e) { OpenFileDialog openfileDialog1 = new OpenFileDialog(); if (openfileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { this.btnChoose2.Text = openfileDialog1.FileName; String filename = DialogResult.ToString(); var excelApp = new Excel.Application(); excelApp.Visible = true; excelApp.Workbooks.Open(btnChoose2.Text); } }