New Post: How to create PivotTable
New Post: How to create PivotTable
Patch Uploaded: #14250
BrunoAltinet has uploaded a patch.
Description:
Some time ago a behaviour for numbers got broken. It starts going in as text if it's not double, which doesn't make much sense.
Hence i corrected that part in the SetValue<T>
Also, i corrected a common nullreferenceexception when seting null as a value to a cell
instead of value.ToString() i've put Convert.ToString(value) .
Same method
Hope that helps and you approve of it.
Great job, thanks!
New Post: Update Link on other files
I've very big .xlsx file. It's contains mane references on other file. How I can update data in my file when open it with ClosedXML?
My problem-data change in other files, but when I open my file in my program,data not changed in this file. Excel has a button "Update all links" and I set "Automatic update"..
I open file
var wb= new XLWorkbook(path,XLEventTracking.Enabled);
New Post: ConditionalFormatting StopIfTrue / Priority
Updated Wiki: Home
This project needs a new caretaker!
As much as I hate to admit it, I can't give this project the love it deserves. If you have the time and want to take care of ClosedXML then send me a message (People tab). Big thanks to everyone who has contributed over the years.
Project Description
ClosedXML makes it easier for developers to create Excel 2007/2010 files. It provides a nice object oriented way to manipulate the files (similar to VBA) without dealing with the hassles of XML Documents. It can be used by any .NET language like C# and Visual
Basic (VB).
What can you do with this?
ClosedXML allows you to create Excel 2007/2010 files without the Excel application. The typical example is creating Excel reports on a web server.
If you've ever used the Microsoft Open XML Format SDK you know just how much code you have to write to get the same results as the following 4 lines of code.
var workbook = new XLWorkbook(); var worksheet = workbook.Worksheets.Add("Sample Sheet"); worksheet.Cell("A1").Value = "Hello World!"; workbook.SaveAs("HelloWorld.xlsx");
Something more elaborate:
The Documentation page has an example of how to create the following table (Showcase) as well as many other examples:
Updated Wiki: Home
This project needs a new caretaker!
As much as I hate to admit it, I can't give this project the love it deserves (paying the bills has a slightly higher priority for me). If you have the time and want to take care of ClosedXML then send me a message (People tab). Big thanks to everyone who has contributed over the years.
Project Description
ClosedXML makes it easier for developers to create Excel 2007/2010 files. It provides a nice object oriented way to manipulate the files (similar to VBA) without dealing with the hassles of XML Documents. It can be used by any .NET language like C# and Visual
Basic (VB).
What can you do with this?
ClosedXML allows you to create Excel 2007/2010 files without the Excel application. The typical example is creating Excel reports on a web server.
If you've ever used the Microsoft Open XML Format SDK you know just how much code you have to write to get the same results as the following 4 lines of code.
var workbook = new XLWorkbook(); var worksheet = workbook.Worksheets.Add("Sample Sheet"); worksheet.Cell("A1").Value = "Hello World!"; workbook.SaveAs("HelloWorld.xlsx");
Something more elaborate:
The Documentation page has an example of how to create the following table (Showcase) as well as many other examples:
Updated Wiki: Documentation
This project needs a new caretaker!
As much as I hate to admit it, I can't give this project the love it deserves (paying the bills has a slightly higher priority). If you have the time and want to take care of ClosedXML then send me a message (People tab). Big thanks to everyone who has contributed over the years.---------------------
Please don't ask questions on the comments section of the documentation pages because Codeplex doesn't send notifications for them. Please use the discussion tab instead. Thanks.
Requirements:
To use ClosedXML you must reference the following DLL: DocumentFormat.OpenXml.dll (part of MS Open XML SDK 2.0)FAQ
How do I deliver an Excel file in ASP.NET?Does it support Excel 2003 and prior formats (.xls)?
How can I insert an image?
Text with numbers are getting converted to numbers, what's up with that?
How do I get the result of a formula?
Examples
Showcase - No nonsense example of how to use this APIBasic Table
Hello World - Proverbial hello world program
Real world scenarios
Finding and extracting the dataTime Savers
Simplifying your life...Performance and Memory
Turning off eventsBetter lambdas
Where to use the using keyword
Other performance improvements
Misc
Data Types - How to handle and convert cell's data types (Text, Boolean, DateTime, Numeric)Creating Multiple Worksheets
Organizing Sheets
Loading and Modifying Files
Using Lambda Expressions
Cell Values
Workbook Properties
Using Formulas
Evaluating Formulas- New
Creating Rows/Columns Outlines
Hide Unhide Row(s)/Column(s)
Freeze Panes
Copying Worksheets
Using Hyperlinks
Data Validation
Hide Worksheets
Sheet Protection
Tab Colors
Conditional Formatting
Inserting Data/Tables
Copying IEnumerable Collections - Using "cell.Value = collection"Inserting Data - Using "cell.InsertData(collection)"
Inserting Tables - Using "cell.InsertTable(collection)"
Adding DataTable as Worksheet
Adding DataSet
Styles
Styles - AlignmentStyles - Border
Styles - Fill
Styles - Font
Styles - NumberFormat
NumberFormatId Lookup Table
Style Worksheet
Style Rows and Columns
Using Default Styles
Using Colors
ClosedXML Predefined Colors
Excel Indexed Colors
Using Rich Text
Using Phonetics
Ranges
Defining RangesMerging Cells
Clearing Ranges
Deleting Ranges
Multiple Ranges
Shifting Ranges
Transpose Ranges
Named Ranges
Accessing Named Ranges- New
Copying Ranges
Using Tables
Sorting Data
Selecting Cells and Ranges
Rows
Row Height and StylesSelecting Rows
Inserting Rows
Inserting and Deleting Rows
Adjust Row Height and Column Width to Contents
Row Cells
Columns
Column Width and StylesSelecting Columns
Inserting Columns
Inserting and Deleting Columns
Adjust Row Height and Column Width to Contents
Column Cells
Page Setup (Print Options)
Pages TabPaper Size Lookup Table
Margins Tab
Headers and Footers Tab
Sheet Tab
Print Areas and Page Breaks
AutoFilters
Adding an AutoFilter to a RangeFilter Values
Custom Filters
Comments
VisibilityPosition
Signatures
Style - Alignment
Style - Colors and Lines
Style - Size
Style - Protection
Style - Properties
Style - Margins
Style - Web
Updated Wiki: Home
This project needs a new caretaker!
As much as I hate to admit it, I can't give this project the love it deserves (paying the bills has a slightly higher priority). If you have the time and want to take care of ClosedXML then send me a message (People tab). Big thanks to everyone who has contributed over the years.
Project Description
ClosedXML makes it easier for developers to create Excel 2007/2010 files. It provides a nice object oriented way to manipulate the files (similar to VBA) without dealing with the hassles of XML Documents. It can be used by any .NET language like C# and Visual
Basic (VB).
What can you do with this?
ClosedXML allows you to create Excel 2007/2010 files without the Excel application. The typical example is creating Excel reports on a web server.
If you've ever used the Microsoft Open XML Format SDK you know just how much code you have to write to get the same results as the following 4 lines of code.
var workbook = new XLWorkbook(); var worksheet = workbook.Worksheets.Add("Sample Sheet"); worksheet.Cell("A1").Value = "Hello World!"; workbook.SaveAs("HelloWorld.xlsx");
Something more elaborate:
The Documentation page has an example of how to create the following table (Showcase) as well as many other examples:
New Post: How to format Fractions
+1/2 + 1/4
to be displayed as 3/4
Under excel formatting this fraction - up to three digits (312/943)
Any help would be great!!!!
Thanks!!!!!!
New Post: How to format Fractions
New Post: How to format Fractions
New Post: How to format Fractions
mCurrWorkSheet.column(1).style.numberformat.format = "# ???/???"
(this would set the format of a whole column)
From what I've seen you can pretty much put any custom format you would use in excel here and it will work.
See the documentation section for a full example of using numberformat.format :
https://closedxml.codeplex.com/wikipage?title=Styles%20-%20NumberFormat&referringTitle=Documentation
New Post: How to format Fractions
Public Function CalcFraction(ByVal eString As String) As String
Dim TestBook As New XLWorkbook
Dim testworksheet = TestBook.Worksheets.Add("test")
testworksheet.Cell("a1").Value = eString
testworksheet.Cell("a1").Style.NumberFormat.Format = "# ???/???"
TestBook.CalculateMode = XLCalculateMode.Auto
Return TestBook.Evaluate(TestBook.Worksheets.Worksheet("test").Cell("a1").GetFormattedString)
End Function
to be given - 1/2 + 1/4 and get 3/4
Thanks for all your help!!!
New Post: Inserting Row works too slow
I have experimented that when using this line of code:
worksheet.LastRow();
then InsertRowsBelow(1) works extremely slow.While I have removed the above calling of LastRow() method then InsertRowsBelow() started to work quickly.
AltWN
New Post: Error saving to memory stream
I'm saving the workbook to a MemoryStream and I'm getting the following error:
"An item with the same key has already been added."
Here's the stack trace:
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary
2.Insert(TKey key, TValue value, Boolean add)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable
1 source, Func2 keySelector, Func
2 elementSelector, IEqualityComparer1 comparer)
at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable
1 source, Func2 keySelector, Func
2 elementSelector)at ClosedXML.Excel.XLWorkbook.GenerateWorksheetPartContent(WorksheetPart worksheetPart, XLWorksheet xlWorksheet, SaveContext context)
at ClosedXML.Excel.XLWorkbook.CreateParts(SpreadsheetDocument document)
at ClosedXML.Excel.XLWorkbook.CreatePackage(Stream stream, Boolean newStream)
at ClosedXML.Excel.XLWorkbook.SaveAs(Stream stream)
__Here's my code:
public static void WriteFileToClient(HttpResponse response, XLWorkbook workbook)
{
response.Clear();
response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
response.AddHeader("content-disposition", "attachment;filename=\"Report.xlsx\"");
using (var memoryStream = new MemoryStream())
{
workbook.SaveAs(memoryStream);
memoryStream.WriteTo(response.OutputStream);
memoryStream.Close();
}
response.End();
}
Can you help?Thanks. :)
New Post: How to create PivotTable
ClosedXML and Excel - How to add a pivot table
Pivot tables can be 90% done using ClosedXML by using a excel template as a base.
Excel template steps
- In excel setup a named table with appropriate header columns and data.
- Create a pivot table as desired using that named table as the source.
- Ensure in pivot options data saved is unchecked and refresh on file load is checked
-
Blank table data leaving 1 blank row in the named table range.
The missing 10% is any new columns are not shown by default, the user will need to pick the columns.
Done!
ASP.Net C# Example:
using (var wb = new XLWorkbook(HostingEnvironment.MapPath("~/App_Data/My_Template.xlsx")))
{
....
region Networth
IXLWorksheet wsNWData;wb.Worksheets.TryGetWorksheet("NWData", out wsNWData);
var nwTable = wsNWData.Table("NWTable");
nwTable.InsertRowsBelow(exportData.Networth.Count - 1, true);
if (exportData.Dates.Count > 2) nwTable.InsertColumnsAfter(exportData.Dates.Count - 2, true); //Handle non-default columns
Then using wsNWData.Cell(x,y) set any new column headers and add data rows
New Post: Code examples on creating Pivot Table?
Here's what I'm doing:
var workbook = new XLWorkbook();
var sheet = workbook.Worksheets.Add("Report");
var source = sheet.Cell(1, 1).InsertTable(Items, true);
var pt = sheet.PivotTables.AddNew("PivotTable", sheet.Cell(1, 5), sheet.Range("A1", "C66"));
pt.RowLabels.Add("Division");
pt.Values.Add("Number");
Where "Items" is an IEnumerable of a class of report data stuff I'm trying to make the table from. Note the manual Range in AddNew() - I started by using source.Range, but that didn't work because it didn't include the table header row. Anyway, this code results in a file with a functional PivotTable; if I leave out either of the last two lines I get an error when I open the file.New Post: Code examples on creating Pivot Table?
Patch Uploaded: #14354
ZickZack has uploaded a patch.
Description:
I have found an Excel Sheet where "WorkbookProperties" are null. And this throws an Exception in private void LoadSpreadsheetDocument(SpreadsheetDocument dSpreadsheet) but Excel 2007 can work without any problem with the same Sheet.
So I think this change will help, please look for the
Remark // dm 20130422 WorkbookProperties might be null