C# (xlApp.工作簿中的xl.Workbook wb) { if(wb.FullName==xlFileName) { 如果(!xlWb) .床单 .Cast() .选择(s=>s.Name) .Contains(名称)) { Show(“Excel工作簿中不存在工作表名称!”); 返回; } xlWs=xlWb.Sheets[xlSheetName]; } } } 捕获(例外情况除外) { //捕捉错误 } } }
添加到Armbrat的解决方案中;这对我很有用:C# (xlApp.工作簿中的xl.Workbook wb) { if(wb.FullName==xlFileName) { 如果(!xlWb) .床单 .Cast() .选择(s=>s.Name) .Contains(名称)) { Show(“Excel工作簿中不存在工作表名称!”); 返回; } xlWs=xlWb.Sheets[xlSheetName]; } } } 捕获(例外情况除外) { //捕捉错误 } } },c#,excel,ms-office,office-interop,excel-interop,C#,Excel,Ms Office,Office Interop,Excel Interop,添加到Armbrat的解决方案中;这对我很有用: // To use currently running instance of Excel Excel.Application objApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); Excel.Workbooks objBooks; Excel.
// To use currently running instance of Excel
Excel.Application objApp =
(Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
Excel.Workbooks objBooks;
Excel._Workbook objBook;
objBooks = objApp.Workbooks; // Adds to the workbooks collection - objBooks.Count will now == 1
objBook = objBooks["Name of workbook"]; // No filename extension, just the name shown in the workbook window title bar
这似乎是对Excel应用程序
app!=空
但应用程序工作簿.Count
仍为0
。为什么它不能获取对已打开工作簿的引用?Excel互操作可能很烦人。最重要的是确保始终释放Excel应用程序对象。否则,将运行EXCEL.exe的重影实例。因此,请关闭所有Excel实例,然后重试该代码。最后,阅读这篇关于如何正确发布Excel的文章,查看此线程以获得正确的清理和讨论:我不认为我的问题与发布应用程序有关,尽管该链接有帮助。。。但我甚至无法访问打开的工作簿。。我可以访问Excel应用程序本身,但找不到要验证的工作簿-您是否检查了在task manager中打开了多少Excel实例?这个答案似乎与问题无关。
app = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
try
{
Microsoft.Office.Interop.Excel.Application app =
System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
}
catch
{
// Excel is not running.
}
String constr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+txtSourceFile.Text+";Extended Properties='Excel 8.0;HDR=YES;';";
String constr2 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtLibrary.Text + ";Extended Properties='Excel 8.0;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbConnection con2 = new OleDbConnection(constr2);
OleDbConnection con3 = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select * From [eudra$]", con);
OleDbCommand oconn2 = new OleDbCommand("Select * From [Sheet1$]", con2);
OleDbCommand oconn3 = new OleDbCommand("Select * From [eudra$] where EXAMPARM in ('with one or more serious adverse events','with one or more non-serious adverse events that met the incidence cutoff')", con);
if (txtSourceFile.Text != "")
{
con.Open();
con2.Open();
con3.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
OleDbDataAdapter sda2 = new OleDbDataAdapter(oconn2);
OleDbDataAdapter sda3 = new OleDbDataAdapter(oconn3);
DataTable data = new DataTable();
sda.Fill(data);
DataTable data2 = new DataTable();
sda2.Fill(data2);
DataTable data3 = new DataTable();
sda3.Fill(data3);
var test = JoinDataTables(data, data2, (row1, row2) => (row1.Field<string>("BODY_SYS").ToUpper() == row2.Field<string>("Term").ToUpper() ));
data3.Merge(test, true);
dgvImp.DataSource = data3;
con.Close();
}
// creating Excel Application
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 = "Exported from gridview";
try
{
// storing header part in Excel
for (int i = 1; i < dgvRESULTS.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dgvRESULTS.Columns[i - 1].HeaderText;
worksheet.Cells[1, i].Interior.Color = System.Drawing.Color.LightYellow;
}
// storing Each row and column value to excel sheet
for (int i = 0; i < dgvRESULTS.Rows.Count - 1; i++)
{
for (int j = 0; j < dgvRESULTS.Columns.Count; j++)
{
if (dgvRESULTS.Rows[i].Cells[j].Value != null)
{
worksheet.Cells[i + 2, j + 1] = dgvRESULTS.Rows[i].Cells[j].Value.ToString();
//worksheet.Cells[i + 2, j + 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(dgvRESULTS.Rows[i].DefaultCellStyle.BackColor);
}
else
{
worksheet.Cells[i + 2, j + 1] = "";
}
}
}
}
catch(NullReferenceException ne)
{
}
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// save the application
workbook.SaveAs(filePath +"\\output.xls", 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();
using Excel = Microsoft.Office.Interop.Excel;
public Excel.Application xlApp;
public Excel.Workbook xlWorkBook;
public Excel.Worksheet xlWorkSheet;
public void ExcelTransferData()
{
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
foreach (Excel.Workbook item in xlApp.Workbooks)
{
//Select the excel target 'NAME'
if (item.Name == "Template.xlsx")
{
xlWorkBook = item;
break;
}
//Select the target workbook
xlWorkSheet = xlWorkBook.Sheets[1] as Excel.Worksheet;
//Set cell value
xlWorkSheet.Cells[5, 1] = "davinceleecode";
}
}
using System.Collections.Generic;
using System.IO;
using System.Linq;
using wf = System.Windows.Forms;
using xl = Microsoft.Office.Interop.Excel;
public static class ExcelTest
{
public xl.Application xlApp = null;
public xl.Workbook xlWb = null;
public xl.Worksheet xlWs = null;
public static bool IsXlFileOpen(string xlFileName)
{
try
{
if (!File.Exists(xlFileName))
{
wf.MessageBox.Show("Excel File does not exists!");
return false;
}
try
{
xlApp = (xl.Application)Marshal.GetActiveObject("Excel.Application");
}
catch (Exception ex)
{
return false;
}
foreach (xl.Workbook wb in xlApp.Workbooks)
{
if (wb.FullName == xlFileName)
{
xlWb = wb;
return true;
}
}
return false;
}
catch (Exception ex)
{
return false;
}
}
public static void GetXlSheet(string xlFileName,
string xlSheetName)
{
try
{
if (!File.Exists(xlFileName))
{
wf.MessageBox.Show("Excel File does not exists!");
return false;
}
xlApp = (xl.Application)Marshal.GetActiveObject("Excel.Application");
foreach (xl.Workbook wb in xlApp.Workbooks)
{
if (wb.FullName == xlFileName)
{
if (!xlWb
.Sheets
.Cast<xl.Worksheet>()
.Select(s => s.Name)
.Contains(xlSheetName))
{
wf.MessageBox.Show("Sheet name does not exist in the Excel workbook!");
return;
}
xlWs = xlWb.Sheets[xlSheetName];
}
}
}
catch (Exception ex)
{
// catch errors
}
}
}
// To use currently running instance of Excel
Excel.Application objApp =
(Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
Excel.Workbooks objBooks;
Excel._Workbook objBook;
objBooks = objApp.Workbooks; // Adds to the workbooks collection - objBooks.Count will now == 1
objBook = objBooks["Name of workbook"]; // No filename extension, just the name shown in the workbook window title bar