Create Excel file based on Multiple Excel files C# - c#

I have three Excel Workbooks such as WorkBookA, WorkBookB and WorkBookC.
Each workbook which has one sheet such as SheetA, SheetB and SheetC.
I want to create a new excel file with SheetA from WorkBookA, SheetB from WorkBookB and SheetC from WorkBookC.
How to do this? Any suggestions..?

This should be very easy.
Grab some code which reads in a particular worksheet into a DataTable. There are plenty of examples showing how to do this, eg.
Excel to DataTable
Create a DataSet containing the three DataTables.
Finally, call my "Export to Excel" library to write this DataSet into a real .xlsx file, Export to Excel

Related

EPPlus - Get file name from worksheet

I am using EPPlus to create Excel files. I need to get the name of the file containing a worksheet from an ExcelWorksheet or ExcelWorkbook object. I can't find any "name" property in the ExcelWorkbook object, or any way of getting from an ExcelWorkbook or ExcelWorksheet back to the containing ExcelPackage.
Is there a property or path of properties I can use to get from ExcelWorksheet to a file name?
To clarify my intent:
I am creating an API that will create table difference reports. It mainly works with ADO.NET DataTables, but it also has adapters that take interop Worksheets or EPPlus ExcelWorksheets, which it will convert to DataTables for processing.
I only need the file name containing the ExcelWorksheet so that it can be printed on the output report for clarity. The API is only really working at the scope of DataTables or objects that are roughly equivalent to tables, like Worksheets or ExcelWorksheets. It doesn't deal with DataSets, Workbooks, ExcelWorkbooks, or ExcelPackages (except when it outputs a report file using EPPlus).
So, I would very much like the API functions to require a bare minimum of parameters, like DataTables, Worksheets, ExcelWorksheets, and some bitflag options. It would also be nice to have the different overloads of the functions take analogous parameters (i.e. taking two DataTables or two Worksheets, or two ExcelWorksheets), and not require extra clutter parameters for the EPPlus input case.
With interop, it is extremely easy to get a file name from a Worksheet (mySheet.Parent.Name), and EPPlus provides easy ways to move down the object hierarchy (myPackage.Workbook.Worksheets[1]), so I assumed there would be some way to move back up the hierarchy from an ExcelWorksheet object.
As suggested by Donald Jansen in the comments, the solution is to edit the EPPlus source code.
Add the following properties to ExcelWorkbook.cs:
//Get the package containing this workbook.
public ExcelPackage Package { get { return _package; } }
//Get the name of the file of this workbook.
public String Name { get { return _package.File.Name; } }
Now you can scale up and down the object hierarchy to your heart's content.

Import Selected CSV file into New Excel file in user defined order

I would like to ask for your help!!
I'm trying to import CSV file into New Excel file, User will give the input to select which CSV file, and program should import purticular data into new excel file and data should be in user defined order.
So guide me to choose correct platform or language to do it.
my sample csv
and my expected output is
Time string will be Row headers,
VarName will be Column headers.
varvalue will be in main data
There are many ways to do this. One of flexible ways is:
Create a model class for your data. (Each record of your csv file will be an instance of your model)
Create a RDLC report that represent your data in your desired layout and format. There you can arrange your data as in rows, columns , pivots, ...
Read data from your csv file and fill in List and pass it as data source to your report.
Export your report to Excel (and even other supported file formats like .doc, .pdf)
Additional resources:
Using a Business Object Data Source with the ReportViewer
RDLC-Export directly to Excel or PDF
Read csv to list ofobjects

Reading in Excel file into array or list

So I have a HUGE excel file with headers, names, values stored.
I'm wanting to read everhting from row 5-95 and column A,B,C,D. I dont want to use a database I want to use a list or array.
What would be a way to read in the excel file and get the info I want?
You can use EasyXLS Excel library.
For reading XLS files use this code:
ExcelDocument xls = new ExcelDocument();
List listRows = xls.easy_ReadXLSSheet_AsList("file.xls", "SheetName", "A5:D95")
For reading XLSX files use this code:
ExcelDocument xls = new ExcelDocument();
List listRows = xls.easy_ReadXLSXSheet_AsList("file.xlsx", "SheetName", "A5:D95")
The listRows will containg the rows and each of this rows is another list that contains the cell values.

Import data from multiple CSV files to an Excel sheet

I need to import data from 50 similar csv files to a single excel sheet.
Is there any way to get only selected columns from each file and put them together in one sheet.
Structure of my csv files: A few columns exactly same in all the files. (I want them to be in excel) then one column with same column name but different data which I want to place next to each other with different names in the excel sheet. I do not not want all other remaining columns from csv files.
In short,
read all csv files,
get all the columns which are common to all csv & put in excel sheet.
Now, take one column from each file which has the same header but different data and put one after the other in excel sheet with named from the csv file name.
Leave remaining rest of the columns.
Write excel sheet to a excel file.
Initially I thought it can be easily done, but considering my programming skill in learning stage it is way difficult for me. Please help.
Microsoft Text Driver allows you to read CSV data in a DataSet, making data manipulation easy.
This Stack Overflow question is a good starting point.
Fastest way could be using FileHelpers to read CSV into a DataTable :
http://filehelpers.sourceforge.net/FileHelpers.CommonEngine.CsvToDataTable_overload_4.html
and then with EPPlus export that DataTable in excel, use method DataTableToExcelXlsx from this snippet:
https://stackoverflow.com/a/9569827/351383
With EPPlus you don't have to have Excel installed on machine that is executing this code, and you can use it on server (ASP.NET)
With a very simple coding, I was able to read the files. Now, the only thing we need to do is to make this code a bit fancy to loop thorough all the CSV files in the folder given and collect data. Once we read the data, it can be filtered and put to an excel as we want.
Of course, excel can import CSV itself, but it is not that practical to do this every time. And again we can add the code to application to use in flexibility, exactly what I am trying to do.
public static System.Data.DataTable GetDataTable(string strFileName)
{
System.Data.OleDb.OleDbConnection dbConnect = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + ";Extended Properties = \"Text;HDR=YES;FMT=TabDelimited\"");
dbConnect.Open();
string strQuery = "SELECT * FROM [" + System.IO.Path.GetFileName(strFileName) + "]";
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, dbConnect);
System.Data.DataSet dSet = new System.Data.DataSet("CSV File");
adapter.Fill(dSet);
dbConnect.Close();
return dSet.dbTables[0];
}

Importing an Excel WorkSheet into a Datatable

I have been asked to create import functionality in my application. I am getting an excel worksheet as input. The worksheet has column headers followed by data. The users want to simply select an xls file from their system, click upload and the tool deletes the table in the database and adds this new data.
I thought the best way would be too bring the data into a datatable object and do a foeach for every row in the datatable insert row by row into the db.
My question is what can anyone give me code to open an excel file, know what line the data starts on in the file, and import the data into a datable object?
Take a look at Koogra.
You instantiate a WorkBook object from a path to an XLS file.
You access a WorkSheet object from the workbook's Sheets property.
You can enumerate over the rows in the worksheet by accessing the sheet's Rows property from index MinRow to MaxRow.
You can enumerate over the cells in a given row by accessing the row's Cells property from index MinColumn to MaxColumn.
Each cell has a Value property (object) as well as a FormattedValue method (string).
Give it a try -- I've found it to be extremely intuitive and easy to use.
You can make use of an OleDbConnection to connect to excel file and the query it using SQL queries.
If it is an Asp.Net application, then you make use of the FileUpload control and get the bytes from the file. Then you will have to manually convert it to a datatable.
Try out these links:
OleDbConnection to excel file
Byte array to datatable
What your looking for is the concept described Here
Providing you dont want to use a third party library anyway, else Dans solution will suit you
First you have to download the dll file namely
NExcel.dll
By using this dll you can make various object which are very useful for
import excel data in .net using both vb as well as c#.
Good luck.

Categories