Enable to read cell value from Excel to C# while - c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Data.OleDb;
namespace EXCEL_SMS
{
class Program
{
static void Main(string[] args)
{
string path = "C:\\Projects\\ExcelSingleValue\\Test.xlsx ";
Application excel = new Application();
Workbook wb = excel.Workbooks.Open(path);
Worksheet excelSheet = wb.Activesheet;
// Read the first cell
string test = excelSheet.Cells[1, 1].Value.ToString();
// string sValue = (range.Cells[2, 4] as Microsoft.Office.Interop.Excel.Range).Value2.ToString();
wb.Close();
}
}
}
I am getting error at activesheet, I want read cell value from Excel while it's open. Can anyone tell how I can achieve it? I'm using Visual Studio 2012.

there is a mistake in "ActiveSheet" please correct it will work fine, in your code it is "Activesheet" ('s' is a small need to be Capital 'S')
Worksheet excelSheet = wb.ActiveSheet; //wb.Activesheet;
Please see the result I am getting with the same program as below, I am able to get the first Cell from test.xlsx

Related

Close Excel in C# after the saving the file

I am using C# to write some information to an Excel file:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.Globalization;
public void OpenExcel(string folder, string filename)
{
this.folder = folder;
this.filename = filename;
path = folder + #"\" + filename;
if (File.Exists(path) != true) //if the output file does not exists, create it
{
var app = new Microsoft.Office.Interop.Excel.Application();
var temp = app.Workbooks.Add();
temp.SaveAs(path);
temp.Close();
}
wb = excel.Workbooks.Open(path);
}
After I am done and have saved the file, I want to close it:
public void Close()
{
wb.Close(false);
excel.Quit();
excel.Application.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
workSheet = null;
wb = null;
excel = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
Both of the voids above are part of a class object named ExcelFile. However, when I use the code above and exit the program I have made, Excel still is left as a background process like in this question: C# closing Excel after using
I have seen that there are a few threads on stackoverflow on this topic, but no solution seems to work for me. I want to avoid killing all Excel processes if possible since this is a program that will make calculations using thread pool that will take hours to make.

Issue when open/save excel files with C#

I am trying to access excel modules with C# and I am having trouble after saving the excel file.
There are duplicate Thisworkbook and Sheets appearing as the picture below.
This only happens when the excel file has Modules while everything will be fine if the excel has only sheets in the project.
Here is the code which is nothing but only opening and saving the same file.
I am using WIN10/ VS2013 /EXCEL 2010.
Does anyone know why will this happen?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Vbe.Interop;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string filepath = #"C:\test.xls";
Excel._Application app = new Excel.Application();
Excel._Workbook workbook = app.Workbooks.Open(filepath);
workbook.SaveAs(filepath);
workbook.Close(true);
app.Quit();
}
}
}

How to copy an excel sheet from workbook A to workbook B

I have the below code to copy Sheet1 from a workbook (test1.xlsx) to another workbook (test2.xlsx). The code doesn't have any errors and it takes forever to execute and I had to stop the code with no change in the files. Please let me know what is wrong.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = Microsoft.Office.Interop.Excel;
namespace Read_from_Excel_file
{
class Program
{
static void Main(string[] args)
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook test1 = xlApp.Workbooks.Open(#"C:\Users\namokhtar\Desktop\test1.xlsx");
Excel.Workbook test2 = xlApp.Workbooks.Open(#"C:\Users\namokhtar\Desktop\test2.xlsx");
test2.Worksheets.Copy(test1.Worksheets["Sheet1"]);
test2.Save();
test1.Close();
test2.Close();
xlApp.Quit();
}
}
}
I think you need to specify where to copy the worksheet to. So at line
test2.Worksheets.Copy(test1.Worksheets["Sheet1"]);
you have to specify which worksheet in test2 you want to copy the worksheet in. test2.Worksheets["whateverworksheetyouwanttooverwrite"].Copy(test1.Worksheets["Sheet1"]);
Source: (https://learn.microsoft.com/nl-nl/visualstudio/vsto/how-to-programmatically-copy-worksheets?view=vs-2017)

Write multiple sheets in excel file using ByteScout Spreadsheet

I have several DataTable objects and I want to write them to one excel file but various sheets.
I'm using bytescout.spreadsheet to work with excel files.
How can I write multiple table sheets to excel file using this tool and C#?
Read the manual: http://bytescout.com/products/developer/spreadsheetsdk/bytescoutxls_working_with_worksheets_in_xls_document.html
using System;
using System.Collections.Generic;
using System.Text;
using Bytescout.Spreadsheet;
namespace Worksheets
{
internal class Program
{
private static void Main(string[] args)
{
// Create new Spreadsheet
Spreadsheet document = new Spreadsheet();
// Add worksheets
Worksheet worksheet1 = document.Workbook.Worksheets.Add("Demo worksheet 1");
Worksheet worksheet2 = document.Workbook.Worksheets.Add("Demo worksheet 2");
// Get worksheet by name
Worksheet worksheetByName = document.Workbook.Worksheets.ByName("Demo worksheet 2");
// Set cell values
worksheet1.Cell(0, 0).Value = "This is Demo worksheet 1";
worksheetByName.Cell(0, 0).Value = "This is Demo worksheet 2";
// Save document
document.SaveAs("Worksheets.xls");
}
}

Creating Excel Spreadsheet - Library Reference issue

I am attempting to create an Excel spreadsheet in C#. I am using VSE 2010. I added a project reference to
Microsoft Excel 14.0 Object Library
and my using statements include
using Microsoft.Office.Interop.Excel;
however, this code gives an error "the type or namespace could not be found" for all the Excel. statements.
Is there anything else I need to be doing?
class excelsheet
{
private static void CreateWorkbook(string FileName)
{
Excel.Application xl = null;
Excel._Workbook wb = null;
Excel._Worksheet sheet = null;
//VBIDE.VBComponent module = null;
bool SaveChanges = false;
}
}
I'm assuming you are doing this:
using Microsoft.Office.Interop.Excel;
Instead, try changing it to a using statement like:
using Excel = Microsoft.Office.Interop.Excel;

Categories

Resources