Reading particular columns of Excel sheet - c#

I need to read particular columns from an Excel sheet (say Columns A,P,Q,B) and also some particular cells (say C3 or D10). I do not need it to be displayed in a DataGrid view or anything (all examples I have seen use DataGrid).
How do I do that and write them into a new CSV file?
I have no sample code as I do not know how to proceed.

I sugest you use the ExcelReaderInterop library.
using Microsoft.Office.Interop.Excel;
Detailed example can be found here:
http://www.dotnetperls.com/excel
I had to deal with a similar issue a few weeks ago and could not find a simpler way to deal with this. The post suggest this overkill approach may be due to lots of legacy code in the library.

We have successfully used the Microsoft Access Database engine to open and read Excel files. The "2010 Access Redistributable" can also be installed on a server free of charge. What you asking for is a multi-step process:
Open a connection to the file using the Access OleDbConnection. In the connection string the "Data Source" is the file name.
Select the appropriate worksheet, which will return a DataTable object.
Grab a row from the data table or iterate over top of all of them myDataTable.Rows.
Access the column in question.
This post shows some of the process:
SSIS Excel Source Connection. What does it use to read Excel?
Hopefully this gets you pointed in the right direction.

I copied the columns that I needed to another excel sheet and saved it as CSV. Then read this csv to perform the task.
This was the easiest option as the machine I was suppose to run the program didn't have Microsoft office.

Related

How to update data in excel File using C#?

This Question might be repeated, But I couldn't get solution regarding my problem so far. I'm new to Interop. I'm using excel file (as a database).
Here is data presentation in excel file
in my data If Card ID repeated then I need to increment '1' in Counter in the same row, similarly I need to fetch IP address of same row..
I'm using Interop Excel approach to insert data in excel file..
Kindly tell me how can I perform that update operation to that excel file through C# (WPF)
Sorry for bad English..
Thanks
I recommend using Closed XML
You write to the file directly and don't need Excel. It will need to be the latest version of an Excel file to work (The open xml standard).
Epplus.dll or npoi.dll will also read/write to excel files w/o excel.
Save the data in an XML or JSON file, then when you want to visualize them you create the excel file from these data, so you will have a very light file and easy to read and update if you wish.
I haven't done this specifically through wpf, but you can access powershell cmdlets through .net and powershell has commands for retrieving and writing Excel data.
That said, my experience has been it's very tedious and inconsistent with bugs. I would tell your client that using an Excel file as a database is impossible and certainly prone to failure in practice.
For one thing you will run into read/write restrictions if it is used by anything else.
If you don't mind to use comercial libraries, you can try to use Aspose.Cells. It has rich cells API and able to work without Excel interop API.

Pull Data from an excel sheet, based on a textbox contents

Okay, I'm currently writing an Audit program for the company I work for to allow us do Audits easier than our current process.
PROBLEM:
I'm struggling to get or find a way for VB to reference a stock file (Excel, can be converted to txt delimited files etc.) with a barcode(column 1), and pull a code(column 2) and the description(column 3) from it and put that data in two other text boxes.
I'm currently using a Windows Form Application, so C#.
Could someone please point me to an article or explain how to go about this?
Option:
Connect to the excel via oledb.
Reading excel file using OLEDB Data Provider
Option:
Convert the excel file to a txt file, and read it with StreamReader.

EPPlus / How to get data from pivot table? Or how to manipulate data easily?

I am wasting very much time on manipulating data in reports. Using pivot table is a good idea but how? I tried some free PivotTable classes but they were lacking subtotals.
Then, another approach. For excel output of reports I am using EPPlus. It also supports pivottable. The problem is some of our customers do not have office(OpenOffice, MicrosoftOffice etc.), so just creating and saving an xlsx file does not work. The only thing I can try with EPPlus is creating an ExcelPackage, filling a worksheet with data, and then creating a PivotTable with data.
I have several questions;
1) From that PivotTable object can I access the output of PivotTable fields and values. (Up to now I could not).
2) Related to the above question... Does an xlsx file contains data about the PivotTables or just the rules of creating PivotTable(Like name of table, sourceRange, rowFields, columnFields, dataFields, aggregate options etc). I have made a small test about this. Steps as following:
Opened a new excel file.
Inserted some raw data.
Created pivot table with the data.
Changed some values of data. (without refreshing pivot table)
Saved and closed the file.
Opened the file back.
In fact my guess was "pivot table would update according to new data", but I was wrong. It did not update. This may be a proof for "xlsx file contains not only rules for a pivot table but also all the values of it". If this is so I have a hope to access that data without saving the file (and I do not need any office programs).
3) Any other approach appreciated.
Thanks in advance
I am by no means an expert on EPPlus but have been working with it for the past few months and can hopefully shed some light on your questions.
If you create a brand new xlsx in EEP, add data to a worksheet, create a pivot table pointed at the data/worksheet, and save it - then the PivotTable does NOT contain any data. It merely contains the definition of the how the PT should slice the data when the file is opened in excel (as you mentioned in one of your questions).
When you actually open the file in excel and SAVE IT what excel does is copies all of the data that the PT relies on and puts it in the pivot table cache. This is why you can then delete the original cells that contained the data, save the file, and then reopen it in excel (might have to dismiss some errors), and still see the PT with data. You can even double click on one of the data cells in the PT and excel will regenerate some or all (depending on which cell you clicked) of the associated data into a new sheet.
Yes, your guess was in fact wrong because of this pivot table cache. You have to tell excel to update the data source in the proper Ribbon (assuming the data is still there) to see the new data show up.
So, to access the data you can figure out where it sits by going into the PivotTable.WorkSheet object and pulling the data out from that. You can see how I did it in the extension method i created here:
Create Pivot Table Filters With EPPLUS
Another option would be to extract the actual worksheet.xml file from the xlsx. An xlsx file (and any other MS Office .???x files) are just ZIP files renamed. So you can use the standard .NET methods to get the xml files out of the zip and use something like LinqToXml to extract the data. So something like this:
var zip = new ExcelPackage(file).Package;
var recordspart = zip.GetPart(new Uri("/xl/worksheets/sheet1.xml", UriKind.Relative));
var recordsxml = XDocument.Load(recordspart.GetStream());
It wont be pretty doing all the XML manipulation but if a final format of XLSX will not work it may be your best option.

Using SQL Bulk Copy to fill a database table

I'm working on an web app using ASP.NET MVC4. I have an Excel file with several columns (some of them are useless) and I'd like to export the data to a related table by uploading the file.
One of my superiors told me to use SQL Bulk Copy to do that. I search some information about that but nothing really interesting (for example, nothing about how to map the excel columns and the table fields).
Any idea/tutorial to do that?
Hello friend please review this article, i think it will contains all the required information you needed. but you need to do some modification as per your requirement.
SQLBulk insert from Excel
if you are facing any connection issue with your Excel workbook, please modified your excel connection string for that there is a good article provide all the necessary information regarding Excel connection string
Excel Connection String Sample

Export Data to Excel without Server-side install

I want to export my data from grid view to the excel. Unfortunately i came to know that there is no excel software on server side. Is there any way that i can still export data in excel format without having excel software on server side.
Thanks.
you can create a CSV format text file, which excel can understand.
This question also outlines some other options you might be able to use, with pros and cons of both.
This must be one of the most asked questions on Stackoverflow.
See Create Excel (.XLS and .XLSX) file from C# For lots of suggestions and a whole list of linked questions.
One trick you can use it to export the html table to the client with the extension of .xls. When the user opens the document with Excel and resaves it will become a real excel document. The fun part is it will retain most of the formatting from the HTML view. This works with HTML/TABLES, I'm not sure about DIV/SPAN/CSS.

Categories