I want to develop a small tool which for open XML file and launch Excel automatically.
The benefit for user who could save the excel file to .xls format very conveniently.
My Dev IDE: Windows XP pro & Visual Studio 2005.
The tool will running at Windows 2000 & Excel 2000. and there is no .net framework installed.
That means i can't code with C#. My choice is C++.
Oneliner:
int main() {
system("Start Excel test.xml");
}
If I recall correctly you want to open an excel file and then auto launch xml editor?
A way is to add a option the the contect menu when rightclicking on the xls file.
Use register for this:
HKEY_CLASSES_ROOT.xls\shell\
create a key (Default) and value something like "Open excel and xml editor"
create a folder "command" and a key (Default) with value "path to your exe" "%L" in that folder.
Then in your app catch the param (which holds the xls)
and then do something like this:
system(<var holding the xls name>);
system(<path to xml editor>):
You could use ShellExecute. It will automatically start program which associated with certain extension, or you could select program manually.
In C#
OpenXML in MSDN - http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.workbooks.openxml(v=office.11).aspx
using Excel = Microsoft.Office.Interop.Excel;
string workbookPath= #"C:\temp\Results_2013Apr02_110133_6692.xml";
this.lblResultFile.Text = string.Format(#" File:{0}",workbookPath);
if (File.Exists(workbookPath))
{
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbook excelWorkbook = excelApp.Workbooks.OpenXML(workbookPath, Type.Missing, Excel.XlXmlLoadOption.xlXmlLoadPromptUser);
}
else
{
MessageBox.Show(String.Format("File:{0} does not exists", workbookPath));
}
Related
I'm working on Visual Studio 2019 community, and I'm writing an MFC application in cpp. I want to add a functionality to the dialog box - such that when pressing a button it converts an existing xls file into pdf file. I've tried using the extension Aspose.Cells.Cpp and Aspose.PDF.Cpp with this code, but I get a lot of errors in the code of the DLL itself. I've also tried installing libHARU from here, but I didn't manage to make it work.
I've tried looking for another way to convert xls to pdf in MFC application in cpp, or another environment for creating desktop applications (which has some basic functionalities such as adding a pressing button in MFC, so I won't have to write everything from scratch with Win32 API), but I didn't find anything. Perhaps someone can explain to me how to perform this conversion in cpp so that I can use it when writing an MFC app, or could refer me to some useful information about another way of creating the application which supports this type of conversions?
You said in your comments to me that you can assume the user has Excel installed on his machine. If so, then you can use Excel's automation object model to manipulate Excel. You could do stuff like #import the type library and stuff...maybe that would make prettier C++. My experience, however, with driving Office apps is to stick more with just raw dispatch pointers. I've had less go wrong that way. This probably needs some more error checking and stuff, but that's up to you.
... and a big caveat. This is for an interactive app...which I halfway assume since you are using the MFC topic. With a service, Microsoft does not recommend you drive their Office applications in this manner. I tested this on one .xlsx file and it worked. Assume it will work for .xls as well. For other formats, you might need to use different import functions.
void ConvertFile(LPCWSTR lpszFileIn, LPCWSTR lpszFileOut)
{
CComDispatchDriver excel;
if (SUCCEEDED(excel.CoCreateInstance(_T("Excel.Application"), NULL, CLSCTX_LOCAL_SERVER)))
{
_variant_t vWorkbooks;
if (SUCCEEDED(excel.GetPropertyByName(L"Workbooks", &vWorkbooks)))
{
CComDispatchDriver workbooks(vWorkbooks.pdispVal);
_variant_t vWorksheet;
if (SUCCEEDED(workbooks.Invoke1(L"Open", &_variant_t(lpszFileIn), &vWorksheet)))
{
CComDispatchDriver worksheet(vWorksheet.pdispVal);
UINT xlTypePdf = 0;
HRESULT hr = worksheet.Invoke2(L"ExportAsFixedFOrmat", &_variant_t(xlTypePdf), &_variant_t(lpszFileOut), NULL);
if (FAILED(hr))
{
_com_error err(hr);
AfxMessageBox(err.ErrorMessage());
}
}
}
excel.Invoke0(L"Quit", NULL);
}
}
I'll give a solution, but this one doesn't use c++, just command line and LibreOffice app:
Use command line Libre Office tool: --convert-to
--convert-to output_file_extension[:output_filter_name] [--outdir output_dir] files
Batch convert files. If --outdir is not specified, then current working directory is used as output_dir.
Eg. --convert-to pdf *.doc
--convert-to pdf:writer_pdf_Export --outdir /home/user *.doc
More details about how to use this command you'll find here: https://help.libreoffice.org/Common/Starting_the_Software_With_Parameters
Later edit: Here is a sample of how to implement the above solution in a C++/MFC code:
STARTUPINFO si = { 0 };
si.cb = sizeof(si);
PROCESS_INFORMATION pi = { 0 };
sCommand.Format(_T("C:\\WINDOWS\\System32\\cmd.exe /c
C:\\LibreOffice\\program\\swriter.exe --convert-to pdf -outdir
c:\\temp\\ c:\\temp\\MyFile.doc"));
::CreateProcess(0, sCommand, 0, 0, TRUE, CREATE_NO_WINDOW, 0, 0, &si, &pi);
I am making an UWP in which I need to access a database. To do so I first downloaded and installed the SQL Universal Windows Platform from this link:
https://sqlite.org/download.html
After this was done I added it as a reference and included it in my code with:
#include <sqlite3.h> //Not sure if I need to make any other changes for this to work
To troubleshoot I have just a button and a textbox. This is the code that it's been run when the button is clicked:
int rc;
sqlite3 *testDB;
if (SQLITE_OK == (rc = sqlite3_open("signers.db", &testDB))) {
Message->Text = "It worked!";
}
else {
Message->Text = "Can't open Database";
}
signers.db it's a database that I created using SQLite so I could have some data to read from my program. Everytime I run the program the text "Can't open Database" appears. I've tried every solution that I see online but none seem to work for me thus I think that I overseeing something. I am fairly new at UWP and also at using databases.
If you need any additional information feel free to ask for it.
If you have the database in your package folder, then it is read-only. You need to use sqlite3_open_v2 and pass the SQLITE_OPEN_READONLY flag.
If you want to open the database for read / write then you need to copy it to your local folder first.
(Also make sure you actually have the database set to deploy; in the Properties window make sure it is set to Content and that it will be copied to the output directory).
According to
https://learn.microsoft.com/en-us/windows/uwp/files/file-access-permissions
you have some limitations to the location where the db file you want to open is located. sqlite itself should work. Try opening a db in a folder specified in the article.
Could also be just a corrupted db file. Did you try to create an empty one?
I have rdl files made by Report Builder 3.0. I need to use them in my Windows application written in C++ in way so that I am able to set SQL Server connection parameters and Report location at runtime.
Is this possible and how?
Google told me that I need to convert rdl to rdlc file, but even then I couldn't find a way of setting these parameters and calling rdlc file from C++.
You can use ReportViewer and use RDL or RDLC files but RDL could have some limitations since it doesn't include necessary information to create data-binding code.
RDL files do not contain some information that the design-time of the
ReportViewer control depends on for automatically generating
data-binding code. By manually binding data, RDL files can be used in
the ReportViewer control.
You can set the parameters from ReportViewer, check this documentation and the below code example in C#:
private void SetReportParameters() {
// Set Processing Mode
reportViewer1.ProcessingMode = ProcessingMode.Remote;
// Set report server and report path
reportViewer1.ServerReport.ReportServerUrl = new
Uri("http://<ServerName>/reportserver");
reportViewer1.ServerReport.ReportPath =
"/AdventureWorks Sample Reports/Employee Sales Summary";
List<ReportParameter> paramList = new List<ReportParameter>();
paramList.Add(new ReportParameter("EmpID", "288", false));
paramList.Add(new ReportParameter("ReportMonth", "12", false));
paramList.Add(new ReportParameter("ReportYear", "2003", false));
this.reportViewer1.ServerReport.SetParameters(paramList);
// Process and render the report
reportViewer1.RefreshReport();
}
This is an example of ReportViewer usage to generate Excel files:
protected void Button1_Click(object sender, EventArgs e)
{
Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string extension;
byte[] bytes = ReportViewer1.LocalReport.Render(
"Excel", null, out mimeType, out encoding,
out extension,
out streamids, out warnings);
FileStream fs = new FileStream(#"c:\output.xls",
FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
Label1.Text = "Report exported to output.xls";
}
Let me know if this can help you.
I have done the below steps, it is working fine on my local machine but when I worked with TFS solution explore below error display (newlines added for clarity):
Error: The character encoding for the file D:\Testcase\data.csv has changed.
Your source control provider may have problems managing files with this type of encoding.
For example, if you save an ANSI-encoded file as UTF-8 you may not be able to merge or show differences.
Steps:
Created data.csv file.
Advance save as a unicode (utf-8 without signature Codepage-65001).
Make data.csv file as copy if new
Code:
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\data.csv", "data#csv", DataAccessMethod.Sequential), DeploymentItem("data.csv"), TestMethod]
public void CodedUITestMethod1()
{
Console.WriteLine(TestContext.DataRow["firstname"].ToString());
// To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
}
Set the Copy to Output Directory in the properties window for that file to Copy Always or Copy if Newer.
How can I mannually terminate an excel application using OLE Automation?
I would like to do this in some exception handling so that an excel process does not remain running if a function throws an error.
Currently I use the below code to open excel:
Variant excel = Variant::CreateObject("Excel.Application");
Like this:
OleVariant excel;
excel = Variant::CreateObject("Excel.Application");
//
// Your code
//
excel.OleProcedure("Quit");