I am working on an Excel 2013/2015 VSTO workbook application. From data entered in a Windows form I'd like to create a new record in an existing Table on one of my worksheets.
How do I insert a new Table row into an Excel Table (not a simple worksheet array) using C# and VSTO?
The Microsoft.Office.Tools.Excel.ListObject provides the needed functionality. The table can be referenced and rows added as follows:
Microsoft.Office.Tools.Excel.ListObject lo = Globals.Sheet1.MyTable;
lo.ListRows.Add();
Optionally, the position of the new row can be specified (https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.listrows.add.aspx). Default is at the end of the table.
IntelliSense did not show the Add() function in my case, which caused the confusion. Just type it in.
Related
I have an excel sheet with dynamicnumber of columns.
I want to read the excel skipping first 2 rows. So starting from rownumber 3 all columns from the excel using OLEDB Data Provider in C# .Net.
Thanks in advance.
If you can change excel file like adding column that is an Id. After you can insert data autoincreasing number.
Column ID screenshot
After you can write an SQL script like 'SELECT * FROM [YourSheet$] WHERE Id>2'.(Id is first row so rowNumber>3 is equals to Id>2)
I am reading an excel file and saving contents to database.
I have two columns , first column name is blank and second column name is A.
Both columns has rows under it .
When I am executing "select * from ["+excel[i]+"]") using oledbconnection,
empty column is automatically replaced with F1. I need both the columns and data as it is available from excel sheet.
How to avoid it?
I don't think there is a way to avoid this. This is a normal behaviour of OleDB when connected to Excel, if it finds empty columns, it will either take the first row as column name or generate some default names as of F1,F2,F3...etc
I am working on a functionality where i upload an EXCEL file and add/update those record(sheet 1) into SQL server. Now i was able to add the data in SQL server with this link.
But what it does, it truncates the table and adds the value again. I don't want to do that because there are 30% of data are generic and can not be deleted. There is field called OSID in excel sheet and same in database.That is the unique key in my table. What i want to do is update only those values in database where it matches with the key from database from the excel sheet.
I would suggest using the code from that link to import the excel data to a separate staging table and update your main table with a join to your staging table.
From that link, the table name they used was tdatamigrationtable. Your update query would look something like
update m set m.col1=s.col1, m.col2=s.col2, m.col3=s.col3
from dbo.mytable m
inner join dbo.tdatamigrationtable s on m.osid = s.osid;
I need to replace table created by OleDbCommand in Excel file. The new table has the same name, but could has different structure. I managed this by deleting sheet via Interop.Excel but there is problem when other sheets refer to this sheet. All formulas are lost. I noticed that creating new table via OleDbCommand creates new Defined name in Names property (Names manager). I've tried to create new table with other name, copy table headers and change name location in:
workbook.Names.Item(ShName, Type.Missing, Type.Missing).RefersTo=oldSheetRange
Where oldSheetRange is column headers range. Unfortunately when I try to insert new data rows they do not insert to the table (no error occurs).
Is there any option to replace table saving reffering formulas?
Regards, Kuba
I'm using VS 2010 with C# for a Windows Form application.
I need to load my data from the Excel sheet onto a DataSet. I created the DataSet using the DataSet designer and I manually added the tables and columns (FirstTable, Column1, Column2). Since I access the columns a lot, the code would be a lot cleaner to have a typed dataset instead of using an untyped one.
When I used the OleDBDataAdapter and populated the DataTable using Fill on FirstTable, Column1 and Column2 were empty and there were two additional columns that were from the Excel sheet (ExcelCol1, ExcelCol2). So unless, I gave the same excel columns names to FirstTable (instead of Column1 and Column2 if I called it ExcelCol1 and ExcelCol2), it would not populate the DataColumns that I created via the designer.
Is there any way to tell the DataSet to ignore the columns coming from Excel and just populate the already defined DataColumns?
IF that is not possible can I somehow connect the Excel sheet via a DataConnection to create the layout of the DataTables? The only thing I'm not sure about is that the excel file is user defined, so the user browses to the excel file to populate the DataSets. BUT the columns for all these excel files will always be the same. Hence I want to preset the layout using a Typed DataSet.
Ok, I think I figured out how to do that. I'm not sure if this is the cleanest method of doing it since I still have to hand-code it to assign the columns names i.e. there is no easy way to ignore the Excel column names but at least I am able to use typed datasets.
This link describes Table and Column mappings which is basically when you would like to use your own column names instead of the ones from Excel.
I would like to point out a few things, so I have given the following code.
Let's assume I have created a DataSet via the DataSet designer called MyDataSet.xsd. It has a table called FirstTable with columns Column1 and Column2. Then the following code can be used for the table mappings
MyDataSet myDS = new MyDataset();
//Some query commands using OleDB to get data from Excel
OleDbDataAdapter myAdapter = new OleDbDataAdapter(<OleDbCommand>);
DataTableMapping tableMap = myAdapter.TableMappings.Add("Table", myDS.FirstTable.TableName);
tableMap.ColumnMappings.Add("ExcelCol1", myDS.FirstTable.Column1.ColumnName);
tableMap.ColumnMappings.Add("ExcelCol2", myDS.FirstTable.Column2.ColumnName);
myAdapter.Fill(myDS);
I realised that when you read data from an Excel file, then in the DataTableMapping statement, the source name is always "Table" even though my sheetname/tablename is different on the Excel file.
To take advantage of the Typed Dataset, you can use commands like myDS.FirstTable.TableName and the same for the columns names instead of,
DataTableMapping tableMap = myAdapter.TableMappings.Add("Table", "FirstTable");
So if you change the table or column names in the DataSet you can use the refactoring utilities of VS.
I hope this helps. If there is a cleaner solution, I would appreciate any help but for now this fixed my problem.