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.
Related
I have a c# function that prints an excel file from it's path. The excel file contains multiple worksheet and I want to be able to select which worksheet to print
private void PrintExcelDocument()
{
var process = new Process();
process.StartInfo = new ProcessStartInfo()
{
WindowStyle = ProcessWindowStyle.Hidden,
Verb = "print",
FileName = Path,
UseShellExecute = true,
CreateNoWindow = true
};
process.Start();
// Fermer le processus s'il est encore ouvert
if (!process.HasExited)
{
process.WaitForExit(5000);
}
process.Close();
}
How can I specify if I want to print all worksheets or one specific ? At the moment, it prints the last one that was opened (when I last saved the file).
I tried adding Arguments with the workbook name but no luck. Also, I haven't found any documentation regarding Excel printing from process. I know I can use Microsoft's dll for Excel (Microsoft.Office.Interop.Excel) but I would like to avoid it if possible.
If you do not like to use Excel Interop COM then this might be helpful to you considering that you are working with .xlsx file format.
If you open an excel file with Hex-editor you will notice that the file signature is PK(zip file format), meaning that it is basically zipped XML files.
Thus, you can just right click the excel.xlsx file, unzip it and then inside '\xl\worksheets' folder you will see 'sheet[1~10].xml' files
Now use the following codes to get all the worksheets
using System.IO;
string[] file_arr = Directory.GetFiles(#"C:\path\to\files\", "*.xlsx");
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.
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.
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)
Is there a way to save changes to an excel spreadsheet through the excel interop (in this case I am adding a worksheet to it) without having it prompt the user if they want to overwrite the existing file with the changes. I do not want the user to even see the spreadsheet open in my application so having a message box popping up asking them if they want to overwrite the file seems very out of place and possibly confusing to the user.
I am using the workbook.SaveAs(fileloaction) method.
Here is where I am initializing the COM reference objects for the excel interop.
private Excel.Application app = null;
private Excel.Workbook workbook = null;
public Excel.Workbook Workbook
{
get { return workbook; }
set { workbook = value; }
}
private Excel.Worksheet worksheet = null;
private Excel.Range workSheet_range = null;
Below is the code I am using to close/save the excel file. The workbook.close() method line is the one that is reportedly throwing the unhandled exception.
workbook.Close(true, startForm.excelFileLocation, Missing.Value);
Marshal.ReleaseComObject(app);
app = null;
System.GC.Collect();
Basically, all you need is ExcelApp.DisplayAlerts = False - Here's how I do it, though:
ExcelApp.DisplayAlerts = False
ExcelWorkbook.Close(SaveChanges:=True, Filename:=CurDir & FileToSave)
Hope this helps
Only this code will Require for stop override alert or Template already in use
ExcelApp.DisplayAlerts = False
I know this is an old post, but I wanted to share a way to make this work without causing possible frustration in the future.
First what I do not like about using: ExcelApp.DisplayAlerts = False
Setting this flag will set this property on the excel file, not just in your program. This means that if a user makes changes to the file and closes it (by clicking the X), they will not be prompted to save the file and will cause frustration later. It will also disable any other prompts excel would typically post.
I like checking if the file exists before saving it:
if (File.Exists(SaveAsName))
{
File.Delete(SaveAsName);
}