Select/Insert/Update/Delete Excel 2007 File and C# - c#

I am using DataGridView and i am binding my Excel file to DataGridView.
I am able to read from the Excel file as well as insert to that file using DataGridView. But I am not able to delete and update that file.
It's giving an error that says:
Deleting data in a linked table is not supported by this ISAM.
Is there any other method to perform these operation in an Excel file using DataGridView. Or simply how can I delete and update a record in an Excel file using DataGridView in C#.

I am afraid that a normal delete (like from access for example) is not supported as mentioned here by microsoft, i think you should consider using MS Access as your data source since it is much easier to deal with or you have to work around a different approach like deleting the while file and recreating it each time for both deleting and updating (not recommended).
In the page I linked they said the best way is to empty the cells for the row u need to delete.
good Luck

Related

How to convert a CSV into a SQLite database using C#

I'm struggling to figure out how to convert a CSV file into a database. I've tried a few methods here but I can't wrap my head around it. I have a CSV file with thousands of rows and I need to convert that into a
SQLite database using C#. Any help is appreciated!
You can leverage MS Excel, open the CSV file and specify your character separator as needed (I believe it will default to tab limited). You can now save your thousands of rows as an Excel spreadsheet versus the character separated file (CSV) format.
Then you can leverage the open source OpenXML libraries to open the Excel document and work with it using object model. In an object oriented fashion, you can programatically create your new database using SQL statements.
Query for the spreadsheet headers to be used as your column names. Of course you'll need to ensure that your source CSV had provided appropriate headers. These can easily be added to the top of the large file if not.
E.g.
https://learn.microsoft.com/en-us/office/open-xml/how-to-get-a-column-heading-in-a-spreadsheet
Next, you simply iterate the rows and construct your SQL statement to insert the rows. You can review the Open XML docs, Microsoft docs, or existing StackOverflow docs for sample code on how this is easily done.
How to read xslx with open XML SDK based on columns in each row in C#?

Modifying Excel Sheet through C# without Interop.Excel

I know questions like this are around in stack and there are 3rd part libraries to do the trick but none of them is fixing my issue at the moment. So the issue.
I have an Excel workbook (.xlsx) with multiple sheets generated by another system. I have to read the data from this via SSIS and dump it to a SQL DB.
Now the issue is although the Excel sheet contains data and when I open manually it opens without any error and the data displays when I use a script task and use OLEDB connection to connect to the excel and open it up the connection is made successfully but when reading data the column names are not picked (I get F1, F2 likewise) and no data rows are read. I simply get a blank row and that's about it. I have tried with HDR= YES and NO and IMEX=1 and 0 but always the result is same.
Funny thing is if I open the excel sheet do some modification (like change a sheet name save and change back the sheet name and save and close) and after that I try to run the package the data gets picked without any issue (also I noticed that the file size increases from 164KB to 196KB). Now because of this what am trying to do is modify the the file a bit and save via code.
So the initial step I tried was through using Office.Interop.Excel and it works like a charm in my machine but on the server NO OFFICE so IT NO WORKS. And nope the IT guys are never going to install access engine or excel or anything there.
Then I tried via OpenXML and 3rd party library like NPOI and even via OLEDB connection to modify the file. in both NPOI and OLEDB methods the file got changed but still it didn't get picked up properly by the SSIS package (I noticed that the file size didn't change and remained at 164kb). In OpenXML it wasn't able to open the file and threw an error saying "the document cannot be opened because there is and invalid part with an unexpected content type".
So right now I am stuck with no proper method in sight and would appreciate any help in solving this either through c# code or any other SSIS method available. SSIS version am using is 2008.
Edit 1
So I noticed that the script task is able to read the data from the first sheet out f the multiple sheets but the other sheets are the problem. So somewhere the xml for these sheets are broken. Anyway I can copy the xml configs of the first sheet to other ones? Just a thought...
Edit 2
So the first sheet is of ContentType "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml" while all the other sheets are of ContentType "application/xml"
Ultimately ended up using two libraries for this. The data was read without an issue by using exceldatareader (http://exceldatareader.codeplex.com/). Using this the data was read into a dataset easily and then it was written to a new Excel file using epplus (http://epplus.codeplex.com/).
After that when the new excel file was read via the SSIS package data got picked without an issue. Hope this will help someone out there.

Export Datagridview records to Excel

Hey friends i need to export records of datagridview of winform to MSExcel. I want to do it without using any DLL i.e., with the buid in properties of C#. So is there any good solution for my problem?
Export the data as CSV file. Excel can read those fine.
A common way to do this would be through MSOffice PIAs. Is that what you'd like to avoid?
You can read the values from data grid using reflection or you can take your data model which is bound to the data grid and create a csv file. The csv file can be read by MS Excel.
I followed this video tutorial, in 10min you can do it, you must use the System.Data.SqlClient reference, make a method, and override, the code for the button, and put the gridview in a form tag, here is the link export from datagridview to excel

How to Export database data table to Excel Sheet in asp.net?

I have a requirement that , i have a table of 400 columns.
Here i need to Export this table to Excel sheet.
And i need to upload to Open Office Sheet. I do not have MS-office.
This is in ASP.NET and C#.NET
I am new to this concept.
So please help me to solve this.
And friends there is a problem , i have 400 columns , which is not possible to insert into Open Office Excel , i need to insert other columns into new sheet
Thank You All!!!!
Have a look at this.
Alternatively you can look at using OleDB to do this
Reading and Writing Excel Spreadsheets Using ADO.NET
I've used this method a number of times and found it to be quick and effective.
3rd Alternative, if it doesn't have to be a true xls/xlsx file and you want lots of control over what the final output looks like, try Microsoft's Spreadsheet XML.
XML Spreadsheet Reference
Again, I've used this method to great effect
You can read from one source and update to another using DataSet, with 2 distinct tOleDb connections.
Here's some code that does that.
CopyData-SqlToExcel-TwoOledbConnections.cs

Programmatically generating Excel workbook with pivots and charts

I need to create a workbook which has a raw data on a sheet and a pivot table on another sheet. The pivot is created from the raw data. Then I need to create a chart with this pivot on the same or new sheet.
How can I do this in C# - and is this possible using VSTO?
Easiest way to do it?
Use Open XML SDK 2.0
Build a pre-generated version of the workbook with raw data, pivottable and pivotchart already created.
Install the OpenXML SDK 2.0 setup package
Use the OpenXML SDK 2.0 productivity tool from this install to open the workbook
Select the root element on the left hand pane, and then right click and select Reflect Code
Done - on the left pane is a complete C# class with the necessary code to generate the file.
Well not quite done as you still need to refactor the code to take into account dynamically adding the required data content, however this will get you 97% of the way if you already know C# fairly well.
I've done something similar to this before, but I didn't do it using C#. I used VBA since the language is already built in to Excel.
My approach was this:
Create a worksheet called "RAW DATA." This worksheet has a QueryTable object in it that can be updated via code in VBA.
I manually created a pivot table based on the QueryTable in the RAW DATA worksheet.
I then added code in VBA so that after RAW DATA was updated, Pivot Table was refreshed with the new data.
This method works really well if the layout of your raw data and your pivot table stays the same. I have a workbook that I made for a cowork that updates multiple sheets with pivot tables based on one set of data. She really likes it because just by clicking one button, she has a refreshed view of all of her data.
If this approach works for you and you'd like more details as to how to implement some of those methods, let me know more details of your situation and I can try to help you out.
One option is to connect to database from Excel and refresh the "Raw data" sheet, via VBA or defining an SQL query in Pivot data source. This is not so great as the user who opens the file must be able to connect to the database.
The other option is to fill the "Raw data" sheet programatically via C#. There are numerous libraries that can help you with that, even some free ones, but you can also do it yourself by using the Excel XML format (SpreadsheetML). You can use the Excel 2003 XML format or the new Open XML Excel format. The latter is far more complicated, but with it you can also take advantage of the OpenXML SDK and the Excel Package API.

Categories

Resources