Generating Excel file with some number of sheets with a single template - c#

i am generating an excel file with some number of sheets. I am using a template .xlt file which have one worksheet. Now I am able to generate the xl file with that template if there is only one worksheet in that excel file. But when I have to create more than one worksheet, I am unable to do that. Then the excel file do not use the template and instead use a blank worksheet. Can anyone point me to some reference where i can use the same template worksheet to create n number of worksheets in the same workbook.

SpreadsheetGear for .NET can do it.
See the "Worksheet with Chart to Multiple Worksheets with Charts" sample on our Excel Reporting Samples page for an example or download the free trial here to try it yourself.
Since the SpreadsheetGear API is very similar to the Excel API, you should be able to adapt the SpreadsheetGear code to work with Excel if you need to.
Disclaimer: I own SpreadsheetGear LLC

Related

C# EPPlus cannot Read Excel Spreadsheet

I am trying to read the data in an .xlsx workbook.
EPPlus is successfully reading the Workbook without any errors. The problem is the Worksheets inside the workbook.
EPPlus seems to not read the worksheets. See screenshot below.
enter image description here
I opened the Workbook that I want to read and found out that all the Cell formats are in "Custom".
enter image description here
I tried to change the cell formats manually to "General" and EPPlus successfully reads the Worksheets.
enter image description here
The thing is that the excel file is coming from a downloaded file via Selenium UI automation. And since this is an automation, I do not want to have a manual intervention wherein I will have to change the cell format to "General" every time I run my auto scripts.
Do you guys know any workarounds / other libraries that can read excel worksheets with cell format as "Custom"?
BTW, I am using EPPlus version 4.5.3.3

How to have multiple sheets in Excel ActiveReports 7?

I can generate one sheet, but I can't find a way to generate multiple sheets in one Excel file.
I tried to use GrapeCity.SpreadBuilder.Workbook but I haven't found a way to make it work.
Thanks in advance.
Thank you for using ActiveReports The excel export in ActiveReports is used to export the report as it appears. in the latest version of ActiveReports we have an Excel transformation device that allows you to break the report over multiple excel sheets.
the following sample of code creates workbook with 2 sheets via SpeadBuilder API:
GrapeCity.SpreadBuilder.Workbook sb = new GrapeCity.SpreadBuilder.Workbook();
sb.Sheets.AddNew();
sb.Sheets[0].Cell(1, 1).SetValue("Sheet #1");
sb.Sheets.AddNew();
sb.Sheets[1].Cell(1, 1).SetValue("Sheet #2");

create a report from template using C# excel OpenXML API

I've been using Microsoft COM model to prepare reports but it is slow and fails if server does not have excel installed. So I am moving to using OpenXML for creating reports from a server process that does a ton of other things as well.
I've a template that contains named ranges for data that I need to change or extend, named charts, named chart series, named worksheets, etc. All my formatting is contained in excel file and all I do in my server side process is populate the file with data.
These are the steps that I need to replicate using Excel OpenXML:
Open an existing sheet and create a new sheet based on it.
Get the worksheet by worksheet name
Get the Named Ranges in that worksheet.
Populate the Named Ranges from data and extend the ranges if required based on data set.
Get Chart by Chart Name
Get Chart Series by Series Title
Set Chart series to the new range
Export Chart to jpeg to be used in html.
Save new File.
Is it possible to do these steps in OpenXML cleanly? Any examples will help me a lot. Or a light weight library that does this. Some of these 3rd party libraries are too big and are more useful in creating new sheets from scratch.
Any help appreciated.
AFAIK, with OpenXml you can do anything.
I recommend a ClosedXml lib, it wraps OpenXML SDK and makes it easier to do your job. Link contains samples and stuff. Hope that helps.

Need to convert Excel sheets into Text (tab delimited ) as in MS Excel 2007 programatically using C#

Ok, here is the problem
MS Excel allows me to save the whole sheet as text tab delimited
I'm using this text in a program , but the user needs to make the previous step manually
I want to automate this step so the user only import the Excel Book into my program , and shows how many sheets inside it letting the user select the desired sheet and then the program will convert this sheet into text (tab delimited ) , any ideas ?
Well... one of the easiest to learn methods would be the use Excel Interop. The downside of this method is it (1.) requires Excel to be installed and (2.) Can get messy very quickly... but generally it makes sense.
I suggest familiarizing yourself with Excel Interop. Now, take note of the SaveAs method for the Workbook object.
Loop through the Worksheets property of the opened Workbook.
Display the sheet names for the user to choose.
Get the desired sheet by name and access the Worksheet object
Use the desired Worksheet's Select method
Use the Workbook's SaveAs method. Be sure to use FileFormat option to make it tab delimited.
Hope that helps. Just remember, it's doable.
There are several options:
OpenXML 2.0 from MS - http://msdn.microsoft.com/en-us/library/bb448854.aspx...
It is a free library that does NOT require office to be installed... you can read + write Office files including Excel and Word (Version 2007 and up...).
VSTO/Office Interop from MS - http://msdn.microsoft.com/en-US/office/hh128771.aspx
this need Office to be installed and works only in desktop apps (NOT services or ASP.NET..) and let's you automate whatever you want...

Using templates in excel with C#

As part of the c# program that I am producing I need to generate 1 workbook containing; 2 different worksheets and a 3rd that could be produced any number of times, what is the most effective way of doing this? I have looked into using templates although I am unsure how to repeat certain worksheets whilst only displaying others once. Any help or advice would be appreciated.
A simple way is to make a hands off template example workbook with the three worksheets. Then make a copy of it. Open both and re-copy worksheet number 3 on to the working workbook as a new worksheet as needed.
In response to the comment:
There are a couple of excel engines in a .net component products our there like spreadsheet gear or aspose cells. But if your application is a windows form based and where the application is guaranteed to run has office you can use office automation. You can't legally use office automation on a web server, but it is just as possible on a web server as on a client desktop. I've used the aspose cells and it's very easy to work with and very capable and a little less expensive than spreadsheet gear, but spreadsheet gear does also have a good reputation. Both of those components have very good documentation on how to do anything with excel. But if you have excel and want to use office automation, be sure to look for example code on the web on how to properly close excel from c# or vb.net. There are some tricks to getting it to close properly.
SpreadsheetGear for .NET has ISheet.CopyAfter / CopyBefore methods which enable you to copy an entire worksheet within a workbook or between workbooks.
You can see an example of duplicating a single worksheet multiple times in the Worksheet with Chart to Multiple Worksheets with Charts sample on the SpreadsheetGear / Excel Reporting Samples page here.
Disclaimer: I own SpreadsheetGear LLC
I have done this before with templates. I would create a template xls with the first two worksheets that you don’t want changed, then add a 3rd worksheet that you could copy to the end of the workbook (as you need more worksheets).
If you know ahead of time how many of the 3rd worksheet you need, then you can copy them to the end and delete the template 3rd slot.
ExcelTemplateManager t = new ExcelTemplateManager(template_path, log_path);
t.CopyWorksheetToEnd(3);
t.CopyWorksheetToEnd(3);
t.RemoveAtIndexWorksheet(3);
t.SetSomeValue(3);
t.SetSomeValue(4);
t.Close();
If you don’t know, then keep the template around to copy it to the end as needed, then when you are done, just remove the 3rd worksheet template.
ExcelTemplateManager t = new ExcelTemplateManager(template_path, log_path);
t.CopyWorksheetToEnd(3);
t.SetSomeValue(4);
t.CopyWorksheetToEnd(3);
t.SetSomeValue(5);
t.RemoveAtIndexWorksheet(3);
t.Close();
I used the Microsoft.Office.Interop.Excel dll to create my ExcelTemplateManger class. The basic idea is to create a copy of the template excel file, and work off the copy. Let me know if you need help setting that part up, but it should be too bad.

Categories

Resources