Google Spreadsheet - Write to Blank Worksheet - c#

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.

Related

What is the easiest way to count .xlsx workbook sheets using c#, NPOI and an XSSF workbook?

I am trying to count the number of sheets in a workbook. The workbook is created using NPOI and there doesn't seem to be a way to count the amount of sheets using the C# version of NPOI?
This is a really tricky thing to both explain and show... But I will give it a try.
What I am trying to do is having an existing excel-file as a template for statistics. This existing excel-file can have different amounts of templates and I need to be able to count these templates to know where to place my new sheets and edit their names.
The sender of the data only has to chose which template-sheet should be filled with which data, and I will then remove the template-sheets from the workbook after all data has been inserted.
What I have tried:
I have read the documentation and searched for information and have tried the following approaches:
getNumberOfSheets - How to know number of sheets in a workbook?
Problem with this approach: The C# version of NPOI doesn't seem to have getNumberOfSheets.
Convert found row-counters into sheet-counters - NPOI - Get excel row count to check if it is empty
Can't really recreate the code to work for sheets as the functionality for sheets and rows are too different.
var sheetIndex = 0;
foreach (var sheet in requestBody.Sheets)
{
if (sheet.TemplateNumber == "")
{
sheetTemplate = templateWorkbook.CreateSheet(sheet.Name);
}
else
{
sheetTemplate = templateWorkbook.CloneSheet(Convert.ToInt32(sheet.SheetTemplate));
if (!templates.Contains(Convert.ToInt32(sheet.SheetTemplate)))
{
templates.Add(Convert.ToInt32(sheet.SheetTemplate));
}
// Do math's to make sure we add the name to the newly created sheet further down the code (I need to actual index here)
}
// Insert statistics
//After inserting statistics:
workingCopy.SetSheetName(sheetIndex, sheet.Name);
foreach (var template in templates)
{
workingCopy.RemoveSheetAt(template);
}
}
You can get number of sheets from NumberOfSheets property in XSSFWorkbook class.

Create Excel bar chart from a data source in C# code instead of a spreadsheet range using Aspose.Cells

I'm trying to use Aspose.Cells to generate a bar chart on a spreadsheet. The data source is an IEnumerable of decimal numbers, which are retrieved from a database. Reading the Aspose.Cells documentation, the only way I've seen to create a bar chart in code is by taking the bar chart's data from a range on a spreadsheet. I want to generate the chart directly from the IEnumerable, without having to actually include the data on a spreadsheet. Is there any way to do this, either using Aspose.Cells or standard .NET libraries? If it's possible to generate a temporary workbook in code, paste the data onto it and use that as a data source (discarding it immediately afterward), I could probably also make that work. Thanks!
Please see the following code. It creates the Bar Chart using Aspose.Cells APIs. Please read the comments inside the code for more help.
The following screenshot shows the output Excel file containing the Bar Chart generated by the code for your reference.
Sample Code in C#
// Create empty workbook.
Workbook wb = new Workbook();
// Access first worksheet.
Worksheet ws = wb.Worksheets[0];
// Add Bar chart in first worksheet.
int idx = ws.Charts.Add(ChartType.Bar, 5, 2, 20, 10);
// Access Bar chart.
Chart ch = ws.Charts[0];
// Add two number series, true means they are vertical.
ch.NSeries.Add("{6,3,1,7}", true);
ch.NSeries.Add("{2,5,7,1}", true);
// Set the category data to show on X-axis.
ch.NSeries.CategoryData = "{Apple,Pear,Orange,Mango}";
// Set the name of first and second series.
ch.NSeries[0].Name = "Cricket";
ch.NSeries[1].Name = "Hockey";
// Save the output in xlsx format.
wb.Save("outputBarChart.xlsx", SaveFormat.Xlsx);
Note: I am working as Developer Evangelist at Aspose

How to add a pivot table filter field using EPPlus and C#

I'm creating an Excel file using the EPPlus library and C#.
In this Excel file there is a sheet with pivot table to which I'd like to add a filter field like in this manually created Excel sheet (Country):
How can I do this using EPPlus?
I did not see your code, but you can do something like this.
var ws = excelPackage.Workbook.Worksheets["index of your worksheet"];
var pivotTable = ws.PivotTables[0]; //assuming that you only have 1
var pivotField = pivotTable.Fields["Country"];
pivotTable.PageFields.Add(pivotField); // should add field into desired place
Hope it´s a little bit helpful. Don´t forget to save your excel file at the end.

C# Excel Add In: Get active worksheet as XML

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.

how to format excel sheet according to property value of an object?

i need to create and download a excel sheet in c# asp.net. I used writing Range. because it is fast. but i need to format the excel sheet. According to a property(usercolor) of the user object, i need to color the row. but when writing to range how can i do that?
i am using this code to write
var startCell = (Range)sheet.Cells[2, 1];
var endCell = new object();
endCell = (Range)sheet.Cells[(usersList.Count + 2), noofcolums];
var writeRange = sheet.get_Range(startCell, endCell);
writeRange.Value2 = data;
data is a TwoDimensionalObject. it is created by user object.
As an additional comment: Do NOT use Excel in a server inevironment. It is slow and Excel might spawn error-windows at any time, causing a hang. This cannot be circumvented in a clean way - even Microsoft agrees and does not support office in a server model.
You might try epplus, a free reading / writing library for excel. It is fast , supports formatting and is way nicer to program than excel interop.
To color any Row in Excel
oRange.get_Range("A1","X1").Interior.Color = System.Drawing.ColorTranslator.ToWin32(Color.Orange);
Hope it helps
I could not find any way to use Object property to map with row color in range writing way. I need to write cell by cell. but it is very slow. So i used creating html file(html table) and convert it to excel document. it is not slow also. Thank you very much for all replies
please refer this Export HTML Table to Excel using ASP.NET

Categories