I have a excel template file (C:\Report\Template\abc.xls).
I need to write a C# console application to do following,
copy the abc.xls file from Template folder and save the same template with different name and different folder "Data" ((C:\Report\Data\new_abc.xls)
load the "new_abc.xls" file into memory and write data (comes from database) to it's specific cell (for example i want to write in cell H17)
Please let me suggest or give me link or code to do this. Thanks!
Copying files is easily done with File.Copy. Simply pass in the source path from which to copy and the destination path to copy to as strings.
Take a look at the Microsoft.Office.Interop.Excel classes for manipulating Excel workbooks. Here's a good code sample to get you started. Also this is another overview.
Related
What I'm really trying to achieve
We have an Excel dashboard which is built to be used in tandem with an Export from our in-house application. The generation of the data Excel file is done with EPPlus and we were convinced that we could use the Excel Dashboard as the "base" file for the generation, so that the exported file contained the dashboard in one worksheet and the dataset in another worksheet.
So the user would receive everything they needed in one single file.
We sort of got going with this thanks to named ranges which dynamically adapt to the size of the dataset, covered here
However, we've found, that since the DataModel in the Excel dashboard file is an OLAP Cube, we can't update the underlying dataset and save it with EPPlus and receive an error about "the cache source is not a worksheet" when trying to save the sheet.
So while we worked on that, we sought out a temporary solution... where we've also hit a snag.
The new problem
The temporary solution we thought of is to distribute the Excel dashboard and the data as two separate Excel files. The Dashboard is distributed to those who need it and the data exports can then be generated from our application.
We thought that the only downside of this would be that it would require the user to rename and place the data file side by side with the Excel Dashboard manually.
However, we are running into an issue where Excel is insisting on using the absolute path to the data file instead of a relative path.
This results in requiring the user to point the source to the data export manually. Which is, apparently, done this way:
Now, I'm showing the entire process to also give some insight into the way we've set it up, since I'm not sure I'm using the right words about the technical details - and perhaps I'm grossly mistaken in the approach.
Overall, the way it works
File A contains the dashboard, some sheets with pivot tables used by the dashboard. The pivot tables are all working off of the aforementioned Data Model in the sheet which is a Cube (we need it to be a cube for some of the functions we use in the pivot tables). The Data Model is based on a named range including all the data in one of the worksheets.
File B is the one generated by our application. The two files were placed side by side when designing the dashboard and mapping the data since I was of the impression that Excel attempted to keep relative file paths when possible. The file consists of a single worksheet containing the exported data.
At this point, the challenge was to automatically pull in data from File B into the worksheet in File A. I did this by going to the Data tab and using the Get Data function to point it to File B and telling Excel to load the data from the specified sheet.
This worked like a charm although the "solution" seems overly complicated.
All was well until we tried to use sheet from another machine/directory. Then we found that the path to File B seemed to be absolute and that the data file could no longer be found.
So, the short question after this long-winded explanation is: "How can I possibly have Excel use a relative path to another file, when importing data from an external file with the "Get Data" function?"
By getting more familiar with Power Query I've been able to come up with a solution to this.
My setup is still as described above.
First, I have a sheet with some "system" values I use various places in the Workbook. I added a field there with the following Excel formula:
=LEFT(CELL("filename");FIND("[";CELL("filename");1)-1)
This provides me with the absolute path to the folder where my File A resides.
I use this value, and concatenate the expected filename of File B which holds the data. The result is an absolute path, pointing to where I expect the data file to be located.
I then added a name range pointing to the exact cell where this value is contained.
Next, I added a new Power Query function like this:
= (rangeName) => Excel.CurrentWorkbook(){[Name=rangeName]}[Content]{0}[Column1]
The function takes the name of a named range as a parameter and spits back the value. I my case I called the function GetValue. This function can now be used in other Power Query scripts.
Finally, I loaded up the Power Query scripts which is responsible for loading the data from the other Excel sheet. In that script, I changed the path of the file to this:
Source = Excel.Workbook(File.Contents(GetValue("FilePath")), null, true),
The single thing to notice here is the call of GetValue("FilePath") for the path parameter of File.Contents. FilePath is the name I gave the range pointing to the cell. All it does, is load the path from my sheet and use that as the path for the Excel sheet holding the data.
A quite convoluted solution, but it works.
I'm trying to:
Delete data from a CSV file using SSIS or C#. If I can't delete data then,
At least delete CSV file using SSIS or C#.
I want to know how to do #1 if it is possible.
If you want to delete all the rows in CSV file by using C#... then follow the below code...
System.IO.File.WriteAllText(#"D:/filename.csv",string.Empty);
it delete all the rows and keep the file as empty...
C#
To remove lines from a flat file, you'll need to read the file and write it out to another file excluding any of the lines you don't want.
Read these two articles, they're short
Reading a Text File One Line at a Time
Writing to a Text File
There may be more clever ways of zipping to a particular location. If it were a fixed width file, then you could calculate where to go and how much to strip but since it's a CSV, you're humped on that accord.
SSIS
I'm assuming the desire to remove the lines is a header or trailer as SSIS before SQL Server 2012 didn't play well with those. In that case, you're going to want to establish 2 connection managers. One will read the entire file in a single column. The second will be your well formatted file output. The same work flow will happen here.
In a data flow task, add a Flat File Source and configure it to use the 1 column connection manager. Add a Derived Column component or a Script Component to enable you to determine whether this row is a keeper or not. Add a Conditional Split off that and shunt only the good rows to a Flat File Destination which uses your strongly typed definition.
Then in your next Data Flow, use a Flat File Source connected to the strongly typed definition and bob's your uncle.
using System.IO;
string path = #"c:\temp\a.txt";
if (File.Exists(path))
{
File.Delete(path);
}
I would like to embed a .txt file into my C# project storing a list values for the user. These values should be configurable and therefore the .txt will have to be edited during runtime. I have found out that Embedded Resources cannot be modified. Is there any other way to do that?
Thank you.
Store the text file as an embedded resource. The first time your program is run, copy the embedded resource to a file on disk, and use it for the configuration. Your users can edit the disk file.
The embedded resource version serves as a default configuration.
You can use your app.config or web.config configuration files.
Normall if you will use large amout of data using a database is recommended. I assume you really need just a .txt document. In your assembly write a procedure that will create that text file if its not present. To be more specific lets say your program is mainProgram.exe. In onload event of mainprogram.exe write a procedure checkTxtFile(). This procedure first will check if there is a txt file in the directory.If the file is not presend it will create it with the desired values.
You can create a XML file using Filestream and using it during runtime or
make your own protocol format and store the data in the file so that no one can change it.
Over to that you can also encrypt and decrypt the file on the fly.
Lists of modifyable, persistable values are best stored in a database. There are many lightweight options to choose from; in this case I think a SqlLite database would suit your purposes best.
As we all know that we can not get the full path of the file using File Upload control, we will follow the process for saving the file in to our application by creating a folder and by getting that folder path as follows
Server.MapPath
But i am having a scenario to select 1200 excel files, not at a time. I will select each and every excel file and read the requied content from that excel and saving the information to Database. While doing this i am saving the files to the Application Folder by creating a folder Excel. As i am having 1200 files all these files will be saved in to this folder after each and every run.
Is it the correct method to follow or not I don't know
I am looking for an alternative solution rather than saving the file to folder. I would like to save the full path of file temporarily until the process was executed.
So can any tell me the best way as per my requirement.
Grrbrr404 is correct. You can perfectly take the byte [] from the FileUpload.PostedFile and save it to the database directly without using the intermediate folder. You could store the file name with extension on a separate column so you know how to stream it later, in case you need to.
The debate of whether it's good or bad to store these things on the database itself or on the filesystem is very heated. I don't think either approach is best over the other; you'll have to look at your resources and your particular situation and make the appropriate decision. Search for "Store images on database or filesystem" in here or Google and you'll see what I mean.
See this one, for example.
I want to select multiple files based on their specific contents from a particular folder and edit their contents. I am using winform in C#.
any idea which are the classes that can be used.
It would be helpful if code given for example.
thanks.
have a look at Directory.GetFiles to get to the names of the files in a directory. If they're text files you can read them via File.ReadAllLines (returnsan array of strings, one per line of the file) or File.ReadAllText (returns a single string containing the entire content of the file).
To save the edited files have a look at File.WriteAllLines or File.WriteAllText.