C# Excel Add In: Get active worksheet as XML - c#

I have a working Word add in, which saves the selected part of the document (including images, tables, etc) as an XML string in a database. The add in can also retrieve previously saved XML strings and insert it into the current position of the currently open document, preserving styling and formatting.
I want to do almost the same for Excel. I want to get an XML string that represents the active worksheet, save that to the database and then later retrieve that XML string and insert it as a worksheet into the active workbook.
The problem is, I can't figure out how to obtain that XML string, nor can I figure out how to insert it as a worksheet. I have been experimenting and searching for hours, but to no avail.
Getting the active sheet seems easy:
dynamic sheet = Globals.ThisAddIn.Application.ActiveSheet;
But I haven't been able to get any further.
For reference, this is how I got the XML string in Word:
Microsoft.Office.Interop.Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection;
string xmlString = currentSelection.XML;
To insert it, I used:
string xmlString = ...; // Read the string for the database.
Microsoft.Office.Interop.Word.Selection currentSelection = Globals.ThisAddIn.Application.Selection;
currentSelection.InsertXML(xmlString);

Take a look in this sample.
In this sample they are converting the Excel Sheet into a DataTable by iterating over the rows and cells and converting the DataTable into a string.

Related

ExcelDataReader in C# - How to reference an individual Cell using row and column cordinates

I'm reading an .xlsx spreadsheet into a C# console app with a view to outputting the content as a formatted xml file (to be picked up by another part of the system further down the line).
The problem with the the .xslx file is that it's a pro-forma input document based on, and replacing, an old paper-based order form we used to provide to customers, and the input fields aren't organised as a series of similar rows (except in the lower part of the document which consists of up to 99 rows of order detail lines). Some of the rows in the header part of the form/sheet are a mixture of label text AND data; same with the columns.
Effectively, what I need to do is to be able to cherry pick data from the initial dozen or so rows in order to poke data into the xml structure; the latter part of the document I can process by iterating over the rows for the order detail lines.
I can't use Interop as this will end up as an Azure function - so I've used ExcelDataReader to convert the spreadsheet to a dataset, then convert that dataset to a new dataset entirely composed of string values. But I haven't been able to successfully point to individual cells as I had expected to be using syntax something like
var cellValue = MyDataSet.Cell[10, 2];
I'd be grateful for any advice as to how I might get the result I need.
A Dataset has Tables and those have Rows which hold ColumnValues
A WorkSheet transforms into a Table (with Columns) and the Cells transform to Rows and column values.
To find the cell value at [10,2] on the first Worksheet do:
var cellValue = MyDataSet.Tables[0].Rows[10][2];
Remember that cellValue will be of type object. Cast accordingly.

C# Aspose.Cells set data to Excel file with format

I have search around the web but could not find any solution.
I have a Excel file used as a template, with a area to parse data from DB.
Among the columns of the Excel file, there is a number column with a custom format.
My code (C#) of setting data to Excel file is as below:
Aspose.Cells.Worksheet ws;
Aspose.Cells.Range rg;
...
rg = ws.Cells.CreateRange("A1", "M10");
var setData = genDataArray(dt); //this function convert Datatable to 2D object array
rg.Value = setData;
Another attempt is also a failure:
ws.Cells.ImportDataTable(dt, false, "A1");
The code works fine. Data is set correctly to the Excel file, but without any format of the Excel file.
When I click the cells of Excel file (after the process), and press Enter, the value is formatted with the style I previously set in the Excel file.
How can i make the Aspose to apply the format I set in the Excel file?
Thank in advance.
After varous attempts, I finally found an solution.
The number column of inputted DataTable have data type of String, so i guess Aspose set it to the Excel file as Text value.
After changing it to Decimal type, the value is correctly formatted.

Reading in Excel file into array or list

So I have a HUGE excel file with headers, names, values stored.
I'm wanting to read everhting from row 5-95 and column A,B,C,D. I dont want to use a database I want to use a list or array.
What would be a way to read in the excel file and get the info I want?
You can use EasyXLS Excel library.
For reading XLS files use this code:
ExcelDocument xls = new ExcelDocument();
List listRows = xls.easy_ReadXLSSheet_AsList("file.xls", "SheetName", "A5:D95")
For reading XLSX files use this code:
ExcelDocument xls = new ExcelDocument();
List listRows = xls.easy_ReadXLSXSheet_AsList("file.xlsx", "SheetName", "A5:D95")
The listRows will containg the rows and each of this rows is another list that contains the cell values.

Retrieving table data from a doc file using c#

I am working on a project which involves getting data from a .doc or a .docx file. The input requirements are in a tabular format. Is it possible to retrieve data from table in a row wise manner or as a dataset.I am using Microsoft.Office.Interop.Word to get the data from the doc file.
You can use the property Tables of the Document interface to get a collection with all the tables in your document. For each Table in this collection you can get the rows and for each row the cells.
I.e. if app is your Application object you can write something like this to get the text contained in each cell(supposing that there is exactly one in your doc):
string aCellText;
foreach (Row aRow in Application.ActiveDocument.Tables[0].Rows)
foreach (Cell aCell in aRow.Cells)
aCellText = aCell.Range.Text;
That is not possible with the word, but if you want something like that than you should put tabular data in you excel file and than you can easily read it in the dataset object....
this is not possible to get the data in dataset object from .doc or .docx file. But if your data is in tabular form and also in the excel sheet than you can retrieve the data in dataset object. MS Word is for documentation purpose and excel is used for maintaining data sheets..

Google Spreadsheet - Write to Blank Worksheet

I'm creating a new worksheet in a Google Spreadsheet using the C# library. After creating the worksheet, I want to write to it. I don't have a LocalName, since it's a new sheet with no header. Has anybody done this before?
newRow.Elements.Add(new ListEntry.Custom
{
Value = value,
LocalName = local
});
For new worksheet, I would suggest using the Cell based feed to add the first row:
https://developers.google.com/google-apps/spreadsheets/#changing_contents_of_a_cell
Simply use the indexes A1 to A{N} to add the header and continue using the list based feed as you used to.

Categories