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();
}
}
}
Related
I am having a linux machine and want system drawing to convert a excel table to image for Free.
Here is my code
using GrapeCity.Documents.Excel;
using System;
using System.Drawing;
using System.IO;
class Program
{
static void Main()
{
//Create a new workbook
var workbook = new GrapeCity.Documents.Excel.Workbook();
Console.WriteLine("Working...");
FileStream fileStream = new FileStream("Test121.xlsx", FileMode.Open);
//Open a xlsx file
workbook.Open(fileStream);
IWorksheet worksheet = workbook.Worksheets[0];
//Export the worksheet to image
worksheet.ToImage("Test121.png");
Console.WriteLine("Done!!");
}
}
but this gives a warning at top of image that it is unlicensed
I also could not use Spire.XLS as my excel file could have more than 200 rows.
btw i am using C#.Net on my ubuntu 20.04 LTS
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.
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)
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
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;