I need to take values form one sheet in one Excel workbook and insert them into another existing workbook.
The values I need to take are the first 6 columns of the first file:
And I want to insert them at the beginning of another book like so
I've been using Spire.Xls to read values from the first sheet and I thought I could just do the same; parse the worksheet, read the values and just paste them into the other sheet, but that wouldn't work because three of the columns I want to copy have the same header "Descripcion" so my parser would only take the values form the first descripcion column and skip the other ones.
Is there any way, using Excel.Interop or maybe Spire itself to copy and paste entire columns between workbooks? Or alternately, is there any way to get all of the 3 "descripcion" values (without rewriting the title of the columns)?
VSTO might be helpful. I've done similar tasks in C#/VSTO.
Perhaps read through: Simple Example of VSTO Excel using a worksheet as a datasource
Related
I create xls/xlsx file from C# using ODBC (with Provider=Microsoft.ACE.OLEDB.12.0). The result table has 4 rows (for example). I open the file with Excel, add 5-th row and save the file. When try to read it from C# over ODBC with SELECT * FROM [table] I get only the original 4 rows without 5th. It seems ODBC stores somewhere in XLS file the number of rows and later reads only them without new data entered from Excel or LibreOffice. Is this known problem and can I solve it? If I create new spreadsheet in Excel, all its rows are read fron C#.
EDIT: I found some useful information. When the XLS file is first created from C#/ODBC, there are 2 tables (sheets). If the table name is TABLE, DataTable sheets = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null) will contain sheets.Rows[0] == "TABLE" and sheets.Rows[1] == "TABLE$". Excel will show only one sheet "TABLE". After edit the changes (5th row) exist only in "TABLE$" sheet.
Are you adding the 5th row by code if yes, could you please share the code lines which you are using for doing the same. There might be following issue in your code.
Save commit not done properly.
Before reading the file connection refresh not done.
I think I found the problem. It seems that internal spreadsheet names created by Excel have "$" sign at the end. The sheet name generated by ODBC are the exact string given in CREATE TABLE. On the other hand, Excel (and LibreOffice) show only one sheet for both TABLE and TABLE$ sheets. If I edit the table in Excel, after save the changes are only in TABLE$. The other sheet TABLE is unchanged. When I do SELECT * FROM [TABLE] the result is from the original ODBC generated table without Excel changes. Now I enumerate the available sheets inside XLS file and if first sheet name does not end with "$" and sheets are more than 1, I add "$" to first sheet name and open the correct table. I suppose ODBC connection string may include option to work with "$"-ending tables...
With ClosedXML I am trying to add data to an existing Excel Sheet In an Existing Table. The easy thing to do is to add a table to an excel sheet below is a quick example of how to do that. What I don't understand is if you already have a Table there that is empty how can you just add to the existing table?
// Add a DataTable as a worksheet
wb.Worksheets.Add(dataTable);
I don't know how this question isn't clear to people. If there is an existing Table (created in Excel by going to "Insert -> Table") and you open an Excel document using ClosedXML, adding data to the next row does not automatically expand the Table.
You can expand it prior to adding the data as such:
IXLTables tsTables = thisSheet.Tables;
IXLTable firstTable = tsTables.FirstOrDefault();
if (firstTable != null)
firstTable.InsertRowsBelow(1);
I had a similar requirement, where I needed to insert data into an existing Table, which had a number of data validations / comments / formula built in.
I found that the easiest solution (for me) was to use the Table's ReplaceData call. This is particularly useful if you provide a name for your Table in the Excel worksheet, as that way you can get a reference to the table directly from the workbook, ie:
var candidateTable = workbook.Table("Candidates");
candidateTable.ReplaceData(candidateData, propagateExtraColumns: true);
Note that the key to getting this to work properly is to set the propogateExtraColumns parameter - this will ensure any formula / etc you have set will automatically be copied to any new rows that are created.
FYI - you can set the Excel Table's name by selecting your table in the worksheet, clicking the Design tab, and then entering a table name:
To add a DataTable to an existing worksheet use this:
wb.Worksheet(1).Cell(1, 1).InsertTable(dataTable);
More info in the documentation.
I tried searching for examples and never i found an example for inserting data into an empty excel.
Insert into [Sheet1$] (columnname1, columnName2) values ("somevalue","somevalue");
If I understand correctly, you want a simple way to create a file that can be read in excel. The simple solution I use many times, when I don't need any advanced features of excel sheets, is a CSV (comma seperated value).
You format your data like this :
COLUMN1,COLUMN2,COLUMN3
ROW1_VALUE1,ROW1_VALUE2,ROW1_VALUE3
ROW2_VALUE1,ROW2_VALUE2,ROW2_VALUE3
Between the lines there are linebreaks. On Windows use \r\n.
You can construct the file any way that you wish, for example :
File.WriteAllText("test.csv","product,price\r\nbook,100\r\ncoffee,500");
This will produce a CSV that can be read in excel.
Excel.Worksheet oSheet;
//------
oSheet.Cells[Row,Column] = "Some Info";
// --- Row & Column starts with 1
i need code in C# to open my excel sheet which contain list of students names ,and open CSV file contain two columns name of same student in excel sheet, and marks of student ... by click on button i want to match between the names and put in the excel sheet every name with his mark. That's all just find names and marks from CSV and put it on next cell of name in excel sheet
Reading from the Excel file:
Reading Excel files from C#
Reading from the CSV file:
http://www.switchonthecode.com/tutorials/building-a-simple-csv-parser-in-csharp
(Google for more)
You'll want to tweak the final output of this stuff, of course, so they can be easily compared and manipulated against one another.
Writing to the Excel file may be a little trickier. You may end up having to use the COM library for it:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ef11a193-54f3-407b-9374-9f5770fd9fd7
But, depending on your needs, you may be able to get away with something simpler:
http://www.codeproject.com/KB/cs/WriteDataToExcel.aspx
Im using ExcelQueryFactory to retrieve the column names of a worksheet using C# 4.0.I'm able to see the list of column names but why do i get an additional set of column names like F12,F100,F20 etc . It happens with any reader i used to read my excel file.
It's not an C# issue, that is an Excel issue.
if your columns do not have column names, Excel will provide names in the format F###, where the number is the number of the column, and F is short for Field (I guess).
Now, if you have any columns in the file that at anytime contained data, or are referenced in formulas, or Excel just thinks they are important, you'll get them in the column list.
Just filter out any columns in that format, of course with the caveat that real columns with real names like F7 will get the short end of the stick there.