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

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.

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.

c# sending data from excel to SQL database

I want to be able to send data from an excel file to an SQL database.
The Excel file only has one column and will always be this way.
I am currently working on a project where people will place excel files into a folder which will need to be read and put into sql, I am able to monitor the folder currently and am able to capture the file name of that file to a variable which i can call to select the excel file.
What i cannot find is a simple excel to sql tutorial that doesnt rely on the import wizard.
If you are able to help please do and if you want any further information please feel free to ask
Thanks alot :)

Read&Write an opened excel file in C# while it is being edited

Good morning!
I have been struggling with a problem for a few weeks and when it seemed that the worst part was solved, now I have a problem with an Excel file.
What I want to do is to read and write an Excel file (Test.xls) by a program (Datahub), and at the same time, read and write the same Excel file by Unity3d using a C# script. I am using Bytescout Spreadsheet in C# to read/write from Excel. I have:
-Cells with references from Datahub, which values are being updated constantly. These values need to be read by Unity3d using C#
-Cells with values written by Unity3d using C#, which values are being read constantly by Datahub
Datahub needs the excel file to be opened in order to read/write but on the other hand, Unity3d through C# needs the file to be closed.
I have tried with a macro in the excel file to autosave it and in C# open the file and close it each time it reads/writes, but it does not work. I also tried to autosave it as a copy and in C# open that copy but it does not work either.
Would you know how to face this problem? Thanks a lot! :)
If you using ByteScout Spreadsheet SDK API then you may use your Excel file as the in-memory database to store values and recalculating other cells like this:
private void ReCalculate()
{
Worksheet worksheet;
worksheet = _spreadsheet.Workbook.Worksheets[0];
worksheet.Cell("A2").Value = System.Convert.ToInt32(TextBox1.Text);
worksheet.Cell("B2").Value = System.Convert.ToInt32(TextBox2.Text);
worksheet.Cell("C2").Formula = TextBox3.Text;
worksheet.Cell("C2").Calculate();
TextBox4.Text = worksheet.Cell("C2").Value.ToString();
}
This way serves well if you already have the spreadsheet with some data and formulas inside cells.
But if you are looking to just have a good way to store and retrive data then you should be better use database as it provides a scalable and reliable way to store large amounts of data (millions of rows), run queries and actually you may 2 or more simultenious connections! Databases are able to provide higher speed because of their internal design (indexes etc) while spreadsheets are better optimized to store data.
XLS, XSLX, CSV and other files are better suited for data export and data import to/from databases.
Disclosure: I'm affiliated with ByteScout

Reading particular columns of Excel sheet

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.

Paste-Link Excel ranges into C# app

I am writing a C# app where I need to paste/link tables/ranges from existing Excel documents.
Functionality that I am looking for is this:
user can select a range of cells in an open Excel doc and do a Copy
user switches to my C# app and does a past-link ... my app shows the table from Excel.
user can edit the source Excel doc - this does not automatically get reflected in the C# app. But I want to provide a Refresh button that when clicked will update the C# app based on the latest data from the linked Excel sheet.
I have figured out how to do a basic copy/paste. I cannot figure out how to do this paste-link. Please note I do not want to ask user in my C# app for any cell ranges..I simply want to do paste-link of what is already in the clipboard...
Any ideas if this can be done...it is all Microsoft so I would be surprised if it can't be.. but I am a C# novice.
Thanks for all input.
I figured it out. Here are the steps.
User copies a range in Excel sheet. It goes to Clipboard in a number
of formats but CSV and ObjectLink formats are of particular interest.
In C# app, trigger a Paste-Link function (this is any button).
Retrieve data from Clipboard using ObjectLink format. This comes out as text which contains:
Excel version identifier
Path to the excel file
The sheet name and the selected range in R1C1 notation
Save the ObjectLink data in your C# app, we will use it later as part of refresh
Retrieve the data from clipboard using CSV format. Parse it out and present in C# app. I converted it to HTML since this is what I am building
Modify the original source excel file - change something in the cells that were part of the original range - save the file.
Go back to C# app, trigger Refresh functionality (this is any button). IN your code do the following:
Using ObjectLink data saved in step 2, open the Excel sheet in the background using Excel Interop API tools. Select the sheet and range. Copy the range programmatically to clipboard.
invoke the same copy from clipboard as used in the last step of 2. Basically get the updated Excel data in CSV format from clipboard and replace the original representation you built during step 1.
This works like a charm although the COM part of opening an excel doc from C# is a bit slow I have to admit.
I have not found any references to this procedure on the net...works for me like a charm.
Cheers.

Categories