Open EXCEL (.xlsx) with password in C# - c#

I try to open password protected .xlsx files (Excel 2007 format) without typing the password manually. I have installed Excel 2003 and the Microsoft Office Compatibility Pack that converts the file when opening.
The following code works, but it prompt for the password.
Microsoft.Office.Interop.Excel.Application ExcelApp;
Microsoft.Office.Interop.Excel.Workbook ExcelWorkbook;
ExcelApp = new Microsoft.Office.Interop.Excel.Application();
Object pwd = "xxx";
Object MissingValue = System.Reflection.Missing.Value;
ExcelWorkbook = ExcelApp.Workbooks.Open("C:\\temp\\test.xlsx",MissingValue, MissingValue, MissingValue,pwd);
If I use the same code to open a .xls File (Excel 2003), it works without prompting for the password. Opening .xlsx files without password protection also works fine.
How is it possible to open password protected .xlsx files without prompting for the password with Excel 2003 and the Microsoft Office Compatibility Pack?
The trick from a similar problem changing the readonly argument (3rd) to true
ExcelWorkbook = ExcelApp.Workbooks.Open("C:\\temp\\test.xlsx",MissingValue, true, MissingValue,pwd);
does not work here.

This may be late, but for any future person with Interop, for me it worked like this:
To open to write
var WFile = new Excel.Application();
Excel.Workbook Wbook = WFile.Workbooks.Open("myFilepath", ReadOnly: false, Password: "mypassword");
To open as read-only
var WFile = new Excel.Application();
Excel.Workbook Wbook = WFile.Workbooks.Open("myFilepath", ReadOnly: true, Password: "mypassword");

The Answer by #J1mm1995 works when trying to open files as readonly, and fails to open some excel files when you want to open the excel file for modification(ReadOnly is set to false).
I understood that this was because the Workbooks.Open() method also expected you to specify the WritePassword. The following code worked for me:
var WFile = new Excel.Application();
Excel.Workbook Wbook = WFile.Workbooks.Open(path, ReadOnly: false, Password: "mypassword", WriteResPassword: "mypassword");

Related

Open Excel with password in C#, and close the excel externally

I am looking for a way to open the password protected Excel file in Excel from my c# program.
Please notice I just need to open it for my user. I am not looking for something like Excel Interop to modify the file.
For a file that has no password, I can simply launch the process start with the full path file name as a parameter. But this one has password in it, so it can't be done like this.
If I use Excel interope to open it, I can open it, but the Excel process will leave there when user close the Excel window.
var excelApp = new Excel.Application();
excelApp.Visible = true;
var wb = excelApp.Workbooks.Open(#"d:\temp\test.xlsx", Password:"1234");
Since user will be modifying the file in the Excel window and then close the windows. This part is out of my control. I simply just want to launch the file, then I don't have to care about it. But by using Excel interope, the COM is actually referecing back to my c# program, so that the whole Excel process can't be disposed completely.
I think I know a workaround for it.
Create a helper C# console application called OpenExcelWorkbookWithPassword.exe which looks like that:
static void Main(string[] args)
{
var excelapp = new Excel.Application();
excelapp.Visible = true;
Excel.Workbook workbook = excelapp.Workbooks.Open(args[0], Password: args[1]);
}
and use this from your main application with the following code:
string path = #"d:\temp\test.xlsx";
string pw = "1234";
System.Diagnostics.Process.Start("OpenExcelWorkbookWithPassword.exe", path + " " + pw);
When the user is closing Excel the Excel.exe process will be terminated even if your main application is still running.

use c# to open workbook with ribbon

I am trying to write a wrapper that will open up an excel doc with macros, pass the password through, enable the macros, enable editing, and save it as a new excel sheet so that the end user can just start working with the template without all the hassle.
here is my code:
using Microsoft.Office.Interop.Excel;
string path = #"\\\\home\\trevor$\\My Documents\\New\\_Blank_1cert.xlsm";
string savepath = #"\\\\home\\trevor$\\My Documents\\New\\"+txtName.Text.ToString()+".xlsm";
Excel.Application templatem = new Excel.Application();
Excel.Workbook templatemwb;
templatemwb = templatem.Workbooks.Open(Filename: path, Password: "news", ReadOnly: false);
Excel.Worksheet worksheet = templatemwb.Sheets["Begin Entry"];
worksheet.Activate();
templatemwb.SaveAs(Filename: savepath, FileFormat: Excel.XlFileFormat.xlOpenXMLWorkbookMacroEnabled);
Everything works as expected except that there is no ribbon at the top of the excel window. I need the ribbon there; so how do I show it?
Set the Visible property on your Application object, templatem.

How can I save an .xlsx file as Unicode text using interop?

I have to build a report making desktop application . I need it to be really user friendly, the reports are in Hebrew so it has to be Unicode and not just a regular .csv file or I get question marks.. I want to receive the file path of the report (like c:\folder\file.xlsx) and be able to save a copy of the original file in another location as a Unicode text file.. Help?
Same problem as this guy.
"I need to use the saveas() from Microsoft.office.interop.excel but I can't figure how to do it "
Add this:
using Excel = Microsoft.Office.Interop.Excel;
Then use this code:
Excel.Application app = new Excel.Application();
try
{
app.DisplayAlerts = false;
app.Visible = false;
Excel.Workbook book = app.Workbooks.Open("D:\\test.xlsx");
book.SaveAs(Filename: "D:\\test.txt", FileFormat: Excel.XlFileFormat.xlUnicodeText,
AccessMode: Excel.XlSaveAsAccessMode.xlNoChange,
ConflictResolution: Excel.XlSaveConflictResolution.xlLocalSessionChanges);
}
finally
{
app.Quit();
}
"D:\\test.xlsx" is your input excel filename and "D:\\test.txt" is your unicode text output filename.

Excel 2013 does not display default sheet after adding in C#

In Excel 2013 I can't add a Worksheet after creating the workbook. My C#-Program did this, but if I set the Application on Visible = true, there is no Worksheet in it. My Program can rename Sheets, copy Sheets, but can't Show me them.
After all is done in my program, it need to be saved, but it doesnt work, because there is nothing displayed what can be saved!
This is my code:
Excel.Application appversion = new Excel.Application();
appversion.Visible = true;
Excel.Workbook dataBook = excelApp.Workbooks.Add();
dataSheet = dataBook.Worksheets[1]; // <- this sheet is not visible
dataSheet.Visible = Excel.XlSheetVisibility.xlSheetVisible; // does not work
dataSheet.Name = Seiten[0]; // but my program continues
After adding the workbook Excel starts with this:
workbook without sheet
But where is the default sheet? Without that, I can't save anything. In Excel 2010 all Sheets I need appear and I can see how my program works.

Open Excel workbook in background using interop

I'm wanting to import data from an Excel workbook without actually displaying the open workbook.
I could have sworn I had used the following code on a previous project and it had worked:
var excelApp = new Excel.Application { Visible = false };
var workbook = excelApp.Workbooks.Open(filePath);
Unfortunately when the workbook opens it is displayed to the user which is unnecessary for this application.
I'm using Microsoft Excel 15.0 Object Library on this project when previously I think it was version 12 or 13. Maybe this is the problem, or is my memory fading and the code is incorrect?
I know this is old but just in case anybody still needs this answer...
excelApp.Visible = false;
(excelApp being the name of the variable used for the excel application)

Categories

Resources