Converting CSV file to Excel spreadsheet using EpPlus - c#

I have a CSV file, I am converting the CSV file into Excel using EpPlus, it is successfully converting to Excel but I have a problem, In the CSV file I have comma separated values in a column, so when EpPlus converts the CSV file into Excel these comma separated values are showing in individual columns in the generated Excel file.
Is there any way to resolve this issue?
This is the code which I am using to convert the CSV to Excel
using (ExcelPackage package = new ExcelPackage(new FileInfo(excelFilePath)))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(worksheetsName);
worksheet = package.Workbook.Worksheets.Add(worksheetsName);
worksheet.Cells["A1"].LoadFromText(new FileInfo(csvFilePath), format, OfficeOpenXml.Table.TableStyles.None, firstRowIsHeader);
package.Save();
}
I am facing one more issue, I have time, date columns in the CSV file (both are individual columns) but in the generated excel file the format of time and date columns are not correct. For example if the time value in the CSV file is "12:00:00 AM" in the output Excel it is showing as 42639
and for the date 11/21/2016 it is showing as 42695. How can I correct the format issue?
One more question, Does EpPlus support XML to Excel spreadsheet conversion?

Related

Reading excel data of custom data type in excel to load in to data set but it's truncating the decimal value using oledb provider

I tried and fed up with the problem.
Actually I have a requirement of excel sheet containing Custom format in Excel.
While loading into dataset the decimal value has 0.123456 but its dispalying as 0.1235.
oledb connection string is having Imex=1, HDR=Yes. I need header and the data has to be displayed as 0.123456.
Can you please help me on this.

C# Aspose.Cells set data to Excel file with format

I have search around the web but could not find any solution.
I have a Excel file used as a template, with a area to parse data from DB.
Among the columns of the Excel file, there is a number column with a custom format.
My code (C#) of setting data to Excel file is as below:
Aspose.Cells.Worksheet ws;
Aspose.Cells.Range rg;
...
rg = ws.Cells.CreateRange("A1", "M10");
var setData = genDataArray(dt); //this function convert Datatable to 2D object array
rg.Value = setData;
Another attempt is also a failure:
ws.Cells.ImportDataTable(dt, false, "A1");
The code works fine. Data is set correctly to the Excel file, but without any format of the Excel file.
When I click the cells of Excel file (after the process), and press Enter, the value is formatted with the style I previously set in the Excel file.
How can i make the Aspose to apply the format I set in the Excel file?
Thank in advance.
After varous attempts, I finally found an solution.
The number column of inputted DataTable have data type of String, so i guess Aspose set it to the Excel file as Text value.
After changing it to Decimal type, the value is correctly formatted.

Create Excel file based on Multiple Excel files 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

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];
}

Categories