Find path of excel file in dll - excel

I have a dll that needs to open an excel file but i cannot seem to create the path to the file. The excel file is a template that must be used to compare with an excel file that the user will browse for and open.
I have used this code previously when using an exe file for my application:
string path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Data\\BulkMaintenanceExample.xls");
but with a dll it does not seem to work. Any Ideas?

Try following to get the assembly path:
var dir = AppDomain.CurrentDomain.BaseDirectory;
or
//get the full location of the assembly with DaoTests in it
string fullPath = System.Reflection.Assembly.GetAssembly(typeof(DaoTests)).Location;
string theDirectory = Path.GetDirectoryName( fullPath );
or
string filePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
return Path.GetDirectoryName(filePath);

Related

Open a workbook without knowing the exact name

so i searched but didnt find something good for my use , i have a folder where i will import an excel file , this excel file will have a different name everytime how i can open it with vba , thank you
You can get the file name using the Dir function and multiple character (*) wildcard.
Const Path As String = "C:\Test"
Dim filename As String
filename = Dir(Path & "\*.xlsx")
If Len(filename) > 0 Then
' Do your work
' Remember 'filename' only holds the file name
' you will need to attach the rest of the path to get the full directory.
End If
Note: If there's only one file in the folder you will not have any issues, however if the folder contains multiple files (matching the above pattern), you will need to either loop or provide additional file name characters to the function.
An example:
File name: daily_report_20190404.xlsx
filename = Dir(Path & "\daily_report_*.xlsx")
Hope this helps.

NPOI produces unreadable content

I'm using NPOI to open an existing Excel file, make modifications, and write it back to disk.
However, when I open the file with NPOI and write it back, the file becomes damaged. Excel complains that the file "contains unreadable content", and asks whether I want to "recover the contents" of the file. When I select OK, it says:
Excel cannot open the file 'test.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
Here is my code:
var excelFilename = "c:\\temp\\simplefile.xlsx";
IWorkbook wb;
using (var fs = File.OpenRead(excelFilename))
{
wb = new XSSFWorkbook(fs);
}
var sheet = wb.GetSheetAt(0);
// For this sample, we don't make any modifications to the file.
// Just opening and writing it back is enough to produce this error.
using (var str = new FileStream(excelFilename, FileMode.Open, FileAccess.ReadWrite))
{
wb.Write(str);
}
"simplefile.xlsx" is an empty excel workbook created by Excel 2010.
What's happening here?

Use informations in TXT file in WSH JScript (Source , path , destination)

I am a beginner in JScript , i did a program to remove a file from folder to another in JScript , but each time i need to change those path if i gave it to another person it depends on their path in their computer. So they ask me to do a txt file which they change only the informations below and then the JScript will change either .
enter code here
the JScript :
MoveFile2Desktop(filespec)
{var fso;
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.MoveFile(filespec, "c:\\windows\\desktop\\");
}
and i have a .txt file which contains
Source: P:\Desktop\name.txt
Destination : P:\Desktop\Folder1\name.txt
How can i read these 2 information and replace it in a JScript ?

Getting path string from Excel in IronPython

I am trying to set working directory through IronPython. Its basically for ANSYS Workbench. I am getting the directory path from excel and i am storing it in a variable in IronPython.
dirpath = worksheet.range["E25"].value
and I am giving this variable value as input path to AbsUserPathName and chdir commands.
dir = AbsUserPathName(dirpath)
os.chdir(dirpath)
But none of it is working, it gives error as expected str, got _comObject
Any help is appreciated.
Assuming you are using Microsoft.Office.Interop.Excel you could use one of the following statements:
dirpath = worksheet.Range["E25"].Text
or
dirpath = worksheet.Cells[25, "E"].Text
or
dirpath = worksheet.Cells[25, 5].Text
Your current statement exposes a COM object from the interop-API that might even represent multiple cells and therefore can not be used by chdir as there is no way of implicitly converting the range to a string.

How to use TARGETDIR in visual c++?

Currently the path inside my code is hardcoded. I want to make it dynamic, base on the users selected installation path.
How can I use TARGETDIR inside my code here:
SHELLEXECUTEINFO info = {0};
info.cbSize = sizeof(SHELLEXECUTEINFO);
info.fMask = SEE_MASK_NOCLOSEPROCESS;
info.lpFile = _T("C:\\PROGRA~1\\APPY\\IECapt.exe");
info.lpParameters = full;
info.nShow = SW_HIDE;
TARGETDIR is the path to the directory where you .EXE file is linked. And it's only available at compile time. You want to get the installation directory, so TARGETDIR is not useful.
GetModuleFileName() gives you the path, where your .EXE has been loaded.
This is just a suggestion......
You can use one button and in the OnButonClick() function add the below codes with your other codes..........
CFileDialog m_IDFile(TRUE);
m_IDFile1.m_ofn.lpstrInitialDir=L"C:\\PROGRA~1\\APPY\\";
if(m_IDFile1.DoModal()==IDOK)
m_cFileName=m_IDFile1.GetPathName();
info.lpFile = _T(m_cFileName);

Resources