I have a table which is in ms word .i need to fill the rows with some data through c#.
I was thinking to fill through array or list or some other data sources.
But the challenge is how to select the first row of the table, there are n number of tables is in my word file.
What type of libary are you using ? Try DocX https://docx.codeplex.com/.
It has great table integration. You can use a template file or a file as a template and create a new table. That way you dont need to worry about table indexin. Or you can use text replacement and fill the tables in the template file whit placeholders.
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 have a sql table with the columns "candidatename", "candidatelocation" and "resume".here resume column have only .docx type files in binary form. from front end I need to enter some words or phrases. My requirement is to get all the records which contain these words(or phrases) in .docx file("resume" column).. Here I'm not getting that how to search the given words with the binary type column.. I need this using asp.net with c# and sql server
You will need to install the Microsoft Filter Pack (http://support.microsoft.com/en-us/kb/945934) which will enable you to create a full text index on the varbinary column you are using to store the .docx document.
I have created a package that will query SQL server and return results to excel, this package should replace existing data on the excel with new data every time it runs.
But Drop table command only clears the headings of excel table not the previous data.
Its appending new data in sheet not deleting the old data.
Drop table I am using this command.
Drop Table `Report`
Go
Create Table :
CREATE TABLE `Report` (
`EmployeeID` Long,
`EmpName` LongText,
`EmpGrp` LongText
)
GO
Please help on this. How to delete data from table in excel.
You can create a template file containing the sheet with the header and a File System Task copies it over the previously exported file before the Data Flow Transformation.
Maybe this solution will suit your needs: Overwrite Excel File in SSIS: Workaround for Excel Connection Manager
One way could be to use 'Script Task' in place of Drop Table to validate if the excel is already present and if yes then delete the existing excel.
`
(File.Exists(Dts.Variables["User::DestinationExcelFilePath"].Value.ToString()))
{
File.Delete(Dts.Variables["User::DestinationExcelFilePath"].Value.ToString());
}
Dts.TaskResult = (int)ScriptResults.Success;
`if
Rest all follow the same process as you are doing right now. Also please note to set DelayValidation property of Control Flow to True for the code to work as expected.
I am working on writing some results of a database query into tables in Word. I have already done the code for accessing/creating objects from the results of my query and find myself a little stuck on writing these to a word template given to me. Its essentially a summary document in which i have to insert tables of the data i pulled from the db in the correct position in the document. So for instance, the document has say 4 section headers and under each header, there is some text after which i have to insert a table. One such header can be like:
School Records
Below you will find a table in which all school records will be listed:
So when i go to print my school record data object, i need a way to somehow insert my data in a table format right below the above line in the word document. Can anyone tell me how i can first find the correct position in the doc and then how you create a table in word from c#?
There is an article about how to insert images at specific position in Word document. Maybe it's useful for you to insert table either.
Add bookmark to the word template and then search for the bookmark and replace it with the image/file.