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.
Related
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?
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
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];
}
I am working on a project which involves getting data from a .doc or a .docx file. The input requirements are in a tabular format. Is it possible to retrieve data from table in a row wise manner or as a dataset.I am using Microsoft.Office.Interop.Word to get the data from the doc file.
You can use the property Tables of the Document interface to get a collection with all the tables in your document. For each Table in this collection you can get the rows and for each row the cells.
I.e. if app is your Application object you can write something like this to get the text contained in each cell(supposing that there is exactly one in your doc):
string aCellText;
foreach (Row aRow in Application.ActiveDocument.Tables[0].Rows)
foreach (Cell aCell in aRow.Cells)
aCellText = aCell.Range.Text;
That is not possible with the word, but if you want something like that than you should put tabular data in you excel file and than you can easily read it in the dataset object....
this is not possible to get the data in dataset object from .doc or .docx file. But if your data is in tabular form and also in the excel sheet than you can retrieve the data in dataset object. MS Word is for documentation purpose and excel is used for maintaining data sheets..
I'm working in VS2005 and I need the C# syntax write to write collection values in an Excel sheet. I initialize the collection like this:
Reports oReports =new Reports();//oReports is a collection
Assuming that this collection has values, how would I write them to Excel?
You could do something quick and dirty if you don't want to faff around learning how to write to Excel - just write out to a .csv file, open it yourself in excel and save as Excel format.
Just output in a stream, such as: (will work if your report values have commas in them too)
using (StreamWriter sw = new StreamWriter(#"C:\file.csv"))
{
StringBuilder csvLine = new StringBuilder();
//Enter your header row here:
csvLine.Append("Header1,Header2,Header3");
sw.WriteLine(csvLine.ToString());
foreach (Report report in Reports)
{
csvLine = new StringBuilder();
csvLine.Append(string.Format("\"{0}\",", report.Value1));
csvLine.Append(string.Format("\"{0}\",", report.Value2));
csvLine.Append(string.Format("\"{0}\"", report.Value3)); //etc
sw.WriteLine(csvLine.ToString());
}
sw.Flush();
}
Loop through the collection and add the collection values to cells in your spreadsheet.
Here's a couple links on how to work with an excel spreadsheet in .Net
http://www.c-sharpcorner.com/UploadFile/thiagu304/ExcelAutomation01052007080910AM/ExcelAutomation.aspx
http://support.microsoft.com/default.aspx?scid=kb;EN-US;302084
Convert your reports to an object array (two dimensional), and assign the array to an excel range