Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all 1877 articles
Browse latest View live

New Post: Use of custom Table style

$
0
0
I have a workbook with an existing custom table style. The first issue I ran into is that ClosedXML throws an error when opening an existing workbook where a table uses a custom style. The error is: "Requested value 'StyleName' was not found." and trace:
   at System.Enum.EnumResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument)
   at System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)
   at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
   at System.Enum.Parse(Type enumType, String value)
   at ClosedXML.Excel.XLWorkbook.LoadSpreadsheetDocument(SpreadsheetDocument  
As I didn't need to have preexisting tables this was okay. However I can't figure out how to apply my custom theme to a new table.

I suspect that it is currently a limitation, and that the error is related. I believe the code is trying to parse the table Style name and find it within the XLTableTheme Enum.

Is this a current limitation? Is there a workaround for this issue? If there is no plan to support custom table styles directly, is it perhaps possible to catch a Parse error on the table style enum and use a 'default' table style - to allow existing workbooks with custom styles to be opened?

Thanks and kind regards,

New Post: Use of custom Table style

$
0
0
After looking at the source code, I can confirm my suspicion regarding the source of the error. While it would be amazing if ClosedXML supported custom table styles, it would likely be beneficial in any case to add some error handling such that existing files with custom-styled tables can be opened. Something like:

XLWorkbook_Load.cs
if (dTable.TableStyleInfo != null)
{
    if (dTable.TableStyleInfo.ShowFirstColumn != null)
        xlTable.EmphasizeFirstColumn = dTable.TableStyleInfo.ShowFirstColumn.Value;
    if (dTable.TableStyleInfo.ShowLastColumn != null)
        xlTable.EmphasizeLastColumn = dTable.TableStyleInfo.ShowLastColumn.Value;
    if (dTable.TableStyleInfo.ShowRowStripes != null)
        xlTable.ShowRowStripes = dTable.TableStyleInfo.ShowRowStripes.Value;
    if (dTable.TableStyleInfo.ShowColumnStripes != null)
        xlTable.ShowColumnStripes = dTable.TableStyleInfo.ShowColumnStripes.Value;
    if (dTable.TableStyleInfo.Name != null)
    {
        XLTableTheme theme;
        if (Enum.TryParse(dTable.TableStyleInfo.Name.Value, out theme))                        
            xlTable.Theme = theme;                        
        else                        
            xlTable.Theme = XLTableTheme.None;                                             
    }
    else
        xlTable.Theme = XLTableTheme.None;
}
In this way the Load will succeed even when a custom style is used on a table in the document.

New Post: Use of custom Table style

$
0
0
Sorry for the string of self replies. After further investigating the source code, it seems that the only reason a custom theme is not supported is that the Theme is using an Enum, even though the final output is just the XLTableTheme name as a string in XML.

My recommendation would be to add a ThemeName property that can be set directly or is automatically set to the Enum.GetName value of the Theme property. This way the Load could fall back on setting the ThemeName property when the enum value cannot be parsed and the Save methods could simply use the ThemeName string rather than the GetName as is currently used.

I will submit a patch.

Commented Unassigned: Pivot Tables Values Problem [9088]

$
0
0
Based on the example [Pivot Tables](https://closedxml.codeplex.com/wikipage?title=Pivot%20Table%20example) , do the following steps to reproduce the problem

Excellent example . It works perfect, but if you add more than one value will have a problem . Here I leave an example to reproduce the problem . Suppose Pastry class has one more property called "Amount" int

Step 1. Add two values

```
// The values ​​in our table will come from the " NumberOfOrders " field
// The default setting is a full calculation of each row / column
pt.Values.Add ("NumberOfOrders");

// Another value ( THIS IS THE ONE THAT CAUSE THE PROBLEM )
pt.Values.Add ("Amount" ) ;

```


Step 2. Save the excel file

Step 3. Open the excel file , and throw the following message

We found a problem with Content ' Patry.xlsx ' . Do you want to try to recover as much content as possible ? If trusts origin of this book , please click yes .

Click yes, and I'll get the following message :

Removed Feature: PivotTable report from / xl / PivotTables / pivotTable.xml part ( PivotTable view)
Records removed : book Properties / xl / workbook.xml part ( Book)


Step 4. Final result , the pivot table is not generated

aTTACH project C# for reproduce problem.

Please expect your help to solve this problem .. thank you very much
Comments: Does anybody know if this issue is going to be fixed? It seems pretty common to Add multiple Columns as values in a pivot table. But anything more than 1 value being added seems to cause errors when dynamically creating a pivot table. I can add either of the below, and I am fine. Trying adding 2, and the file opens with an error. pt.Values.Add("Quantity").SetSummaryFormula(XLPivotSummary.Sum); pt.Values.Add("Total Price").SetSummaryFormula(XLPivotSummary.Sum);

Created Unassigned: Can not create more than 1 Pivot Table in WorkBook. [9397]

$
0
0
I can easily create two Worksheets, and I can easily add a Pivot Table to one of the worksheets. But if you try to add a second Pivot Table to the second Worksheet, you end up with a corrupted excel document.

Below is the method I am calling. It works great once, but not twice.

When I call this method, wsPivot is a new worksheet. I have tried using a different dataRange, or the same dataRange, but either way fails. The new worksheets are there, and work without issue if I don't add a pivot table to them.

I will try to dive into the XML to find the problem, but that is as we all know challenging. Hope somebody has a work around, I really need this fix to continue using this library. Thanks.

private static void CreatePivotTable(IXLRange dataRange, IXLWorksheet wsPivot, string pivotTableName)
{
var pt = wsPivot.CreatePivotTable(wsPivot.Cell(3, 1), pivotTableName);
pt.SourceRange = dataRange;

// OR
// var pt = wsPivot.PivotTables.AddNew(pivotTableName, wsPivot.Cell(3, 1), dataRange);

pt.RowLabels.Add("UnitNames");
pt.Values.Add("TotalPrice").SetSummaryFormula(XLPivotSummary.Sum);
}

New Post: Get value error

New Post: Could not load file or assembly 'ClosedXML'

$
0
0
Hi,

I have used Nuget to install CloseXML into my ASP.NET project. No issues there. Also, in the codebehind, I have added the code and all seems OK in rebuild and deploy. However, when I click on the Export button to export to excel, an exception is thrown.

Appreciate help to clarify why it is saying "file not found" ??

Server Error in '/' Application.
Could not load file or assembly 'ClosedXML, Version=0.75.0.0, Culture=neutral, PublicKeyToken=fd1eb21b62ae805b' or one of its dependencies. The system cannot find the file specified.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'ClosedXML, Version=0.75.0.0, Culture=neutral, PublicKeyToken=fd1eb21b62ae805b' or one of its dependencies. The system cannot find the file specified.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


[FileNotFoundException: Could not load file or assembly 'ClosedXML, Version=0.75.0.0, Culture=neutral, PublicKeyToken=fd1eb21b62ae805b' or one of its dependencies. The system cannot find the file specified.]
InnoCERTP2.PDWorkspace.Layouts.InnoCERTP2.PDWorkspace.OnSiteAuditReport.btnExport_Click(Object sender, EventArgs e) +0
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +115
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +140
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981


Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5483

Created Unassigned: System.Runtime.InteropServices.ExternalException while Adding DataTable to WorkSheet [9398]

$
0
0
Hi,

Somehow while converting DataTable to WorkSheet, I am getting this exception. Not sure what exactly is causing this.

```
A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+.
at System.Drawing.Graphics.MeasureString(String text, Font font, SizeF layoutArea, StringFormat stringFormat)
at System.Drawing.Graphics.MeasureString(String text, Font font, Int32 width, StringFormat format)
at ClosedXML.Utils.GraphicsUtils.MeasureString(String s, Font font)
at ClosedXML.Excel.FontBaseExtensions.GetWidth(IXLFontBase fontBase, String text, Dictionary`2 fontCache)
at ClosedXML.Excel.XLColumn.AdjustToContents(Int32 startRow, Int32 endRow, Double minWidth, Double maxWidth)
at ClosedXML.Excel.XLColumn.AdjustToContents(Int32 startRow, Int32 endRow)
at ClosedXML.Excel.XLColumns.<>c__DisplayClasse.<AdjustToContents>b__d(XLColumn c)
at System.Collections.Generic.List`1.ForEach(Action`1 action)
at ClosedXML.Excel.XLColumns.AdjustToContents(Int32 startRow, Int32 endRow)
at ClosedXML.Excel.XLWorksheets.Add(DataTable dataTable, String sheetName)
at XXXXX A first chance exception of type 'System.Runtime.InteropServices.ExternalException' occurred in XXXXX
```

Code Sample:
```
// Creating Excel workbook
var wb = new XLWorkbook(XLEventTracking.Disabled);
var dataSheet = wb.Worksheets.Add(dataTable, "Data");
```

The DataTable includes some null or empty cells as well as strings in different scripts like Chinese, Arabic, etc.

Any idea what might be the reason for this.



Created Unassigned: Generate binary identical file for same input [9399]

$
0
0
Currently when I generate a workbook with a sheet and save it as .xlsx and then use the same code again I get two files with different content.
I would like to get exactly the same file, so I can easily (e.g. for version control) see that no changes occurred in the spreadsheet.

When I renamed the generated files to .zip and extracted those I noticed the only differences were:
* a different file name for the .psmdcp file generated in package\services\metadata\core-properties
* the content of _rels/.rels (which I believe is just the different file name of the .psmdcp file)

I believe the file name is a GUID. Would it be possible to always use the same one for this file?

Edited Unassigned: Generate binary identical file for same input [9399]

$
0
0
Currently when I generate a workbook with a sheet and save it as .xlsx and then use the same code again I get two files with different content.
I would like to get exactly the same file, so I can easily (e.g. for version control) see that no changes occurred in the spreadsheet.

When I renamed the generated files to .zip and extracted those I noticed the only differences were:
* a different file name for the .psmdcp file generated in package\services\metadata\core-properties
* the content of _rels/.rels (which I believe is just the different file name of the .psmdcp file)

I believe the file name is a GUID. Would it be possible to always use the same one for this file?

Edited Unassigned: Generate binary identical file for same input [9399]

$
0
0
Currently when I generate a workbook with a sheet and save it as .xlsx and then use the same code again I get two files with different content.
I would like to get exactly the same file, so I can easily (e.g. for version control) see that no changes occurred in the spreadsheet.

When I renamed the generated files to .zip and extracted those I noticed the only differences were:
* a different file name for the .psmdcp file generated in package\services\metadata\core-properties
* the content of the .psmdcp file contains one element whose value changes 'dcterms:modified'
* the content of _rels/.rels:
* file IDs that are different (even for files that are the same, such as workbook.xml)
* the different file name of the .psmdcp file

My questions/requests:
* I believe the file name is a GUID. Would it be possible to always use the same one for the .psmdcp file?
* I do not see any usage of the relationship ID. Would it be possible to use the same IDs always?
* Is there some way we can set/force the 'dcterms:modified' element?

Edited Unassigned: Generate binary identical file for same input [9399]

$
0
0
Currently when I generate a workbook with a sheet and save it as .xlsx and then use the same code again I get two files with different content.
I would like to get exactly the same file, so I can easily (e.g. for version control) see that no changes occurred in the spreadsheet.

When I renamed the generated files to .zip and extracted those I noticed the only differences were:
* a different file name for the .psmdcp file generated in package\services\metadata\core-properties
* the content of the .psmdcp file contains one element whose value changes 'dcterms:modified'
* the content of _rels/.rels:
* Relationship IDs that are different (even for files that are the same, such as workbook.xml) for some, but identical for others (e.g. app.xml)
* the different file name of the .psmdcp file

My questions/requests:
* I believe the file name is a GUID. Would it be possible to always use the same one for the .psmdcp file?
* I do not see any usage of the relationship ID. Would it be possible to use the same IDs for all Relationships always?
* Is there some way we can set/force the 'dcterms:modified' element?

New Comment on "Adding an AutoFilter to a Range"

$
0
0
>> // Your can turn off the autofilter with: worksheet.AutoFilter.Clear() Same here, and I need to turn off the filters that are automatically created on all columns

New Post: Disable AutoFilter in Table

$
0
0
This is throwing an "Object reference not set to an instance of an object" exception:
ws.AutoFilter.Clear();
I'm using version 0.75.0.0.

Commented Unassigned: Failed to create a very large file [9384]

$
0
0
Failed to create a very large file

When I have more than 100000 rows gives me the following error

__Row = 183 706__ - System.OutOfMemoryException: An unhandled exception of type 'System.OutOfMemoryException'. in DocumentFormat.OpenXml.OpenXmlChildElements.GetEnumerator () in DocumentFormat.OpenXml.OpenXmlCompositeElement.WriteContentTo (XmlWriter w) at DocumentFormat.OpenXml.OpenXmlElement.WriteTo (XmlWriter xmlWriter) at DocumentFormat.OpenXml.OpenXmlCompositeElement.WriteContentTo (XmlWriter w) at DocumentFormat.OpenXml .OpenXmlElement.WriteTo (XmlWriter xmlWriter) at DocumentFormat.OpenXml.OpenXmlCompositeElement.WriteContentTo (XmlWriter w) at DocumentFormat.OpenXml.OpenXmlElement.WriteTo (XmlWriter xmlWriter) at DocumentFormat.OpenXml.OpenXmlCompositeElement.WriteContentTo (XmlWriter w) at DocumentFormat.OpenXml.OpenXmlPartRootElement .WriteTo (XmlWriter xmlWriter) in DocumentFormat.OpenXml.OpenXmlPartRootElement.SaveToPart (OpenXmlPart openXmlPart) in DocumentFormat.OpenXml.OpenXmlPartRootElement.Save () in DocumentFormat.OpenXml.Packaging.OpenXmlPackage.SavePartContent (OpenXmlPart part) in DocumentFormat.OpenXml.Packaging.OpenXmlPackage .TrySavePartContent (OpenXmlPart part) in DocumentFormat.OpenXml.Packaging.OpenXmlPackage.SavePartContents () in DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose (Boolean disposing) in DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose () in ClosedXML.Excel.XLWorkbook .CreatePackage (String filePath) at ClosedXML.Excel.XLWorkbook.SaveAs (String file) in WebApplication.Reporte12.Obsolecencia.GenerarExcel (Int64 IdSociedad) in ....

according to the program when I add a new sheet the range is __[ClosedXML.Excel.XLWorksheet] = {'Sheet1'! A1: XFD1048576} __

the error is when __wb.SaveAs(file);__
Comments: If the problem is now only with a webform application. Make sure the IIS thread pool is not set to a 32bit application. See https://help.webcontrolcenter.com/kb/a1114/how-to-enable-32-bit-application-pool-iis-7-dedicatedvps.aspx except you want to make sure Enable 32Bit = false. If you application does require 32bit, then you may have to have your ClosedXML generation code run in its own thread pool. Hope this helps.

New Post: Change header of table

$
0
0
Hi,

I am still getting error and I took source code. I get error:

<?xml version="1.0" encoding="UTF-8" standalone="true"?>

-<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">

<logFileName>error103600_01.xml</logFileName>

<summary>Errors were detected in file 'D:\TemporaryReports\Test.xlsx'</summary>


-<repairedRecords summary="Following is a list of repairs:">

<repairedRecord>Repaired Records: Table from /xl/tables/table.xml part (Table)</repairedRecord>

</repairedRecords>

</recoveryLog>

Could you please advise?

New Post: Change header of table

$
0
0
What are you doing mafalarz? Are you using the source code? (I haven't released the fix yet)

Commented Unassigned: Failed to create a very large file [9384]

$
0
0
Failed to create a very large file

When I have more than 100000 rows gives me the following error

__Row = 183 706__ - System.OutOfMemoryException: An unhandled exception of type 'System.OutOfMemoryException'. in DocumentFormat.OpenXml.OpenXmlChildElements.GetEnumerator () in DocumentFormat.OpenXml.OpenXmlCompositeElement.WriteContentTo (XmlWriter w) at DocumentFormat.OpenXml.OpenXmlElement.WriteTo (XmlWriter xmlWriter) at DocumentFormat.OpenXml.OpenXmlCompositeElement.WriteContentTo (XmlWriter w) at DocumentFormat.OpenXml .OpenXmlElement.WriteTo (XmlWriter xmlWriter) at DocumentFormat.OpenXml.OpenXmlCompositeElement.WriteContentTo (XmlWriter w) at DocumentFormat.OpenXml.OpenXmlElement.WriteTo (XmlWriter xmlWriter) at DocumentFormat.OpenXml.OpenXmlCompositeElement.WriteContentTo (XmlWriter w) at DocumentFormat.OpenXml.OpenXmlPartRootElement .WriteTo (XmlWriter xmlWriter) in DocumentFormat.OpenXml.OpenXmlPartRootElement.SaveToPart (OpenXmlPart openXmlPart) in DocumentFormat.OpenXml.OpenXmlPartRootElement.Save () in DocumentFormat.OpenXml.Packaging.OpenXmlPackage.SavePartContent (OpenXmlPart part) in DocumentFormat.OpenXml.Packaging.OpenXmlPackage .TrySavePartContent (OpenXmlPart part) in DocumentFormat.OpenXml.Packaging.OpenXmlPackage.SavePartContents () in DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose (Boolean disposing) in DocumentFormat.OpenXml.Packaging.OpenXmlPackage.Dispose () in ClosedXML.Excel.XLWorkbook .CreatePackage (String filePath) at ClosedXML.Excel.XLWorkbook.SaveAs (String file) in WebApplication.Reporte12.Obsolecencia.GenerarExcel (Int64 IdSociedad) in ....

according to the program when I add a new sheet the range is __[ClosedXML.Excel.XLWorksheet] = {'Sheet1'! A1: XFD1048576} __

the error is when __wb.SaveAs(file);__
Comments: You could also try deploying to an instance of IIS and not the built in ASP.NET development server.

Commented Unassigned: WorkSheet CopyTo doesn't transform Named Ranges addresses [9270]

$
0
0
I'm trying to replicate a Worksheet several time in a Workbook and I'm doing using the following code:
```
IXLWorksheet worksheet = modelSheet.CopyTo("SheetName");
```
When I open the generated document the NamedRanges were copied, using the same name as expected, but all of them are associated to the model worksheet (source). I was expecting the NamedRange address to be changed to the target Worksheet too.

Example:
```
"=ModelSheet!$B$10:$B$10" would transform to "=TargetSheet!$B$10:$B$10"
```

Is there a way to do it or isn't it supported yet?

Many Thanks & great work my man, incredible Lib!
Cheers!
Comments: I found a few issues with Rynkadink's solution: 1. foreach loop is looking for named ranges in targetWorksheet, which doesn't have any named ranges because they were not copied by CopyTo method. That was the original issue anyway. Body of this loop will never execute. 2. Ignoring first issue with this solution, the code is modifying original named range instead of creating a copy. That means these named ranges will move to target sheet (instead of copy). 3. Finally, we have to take care of scope as well. Two named ranges cannot have same name if their scope is 'workbook'. We can solve this by giving them 'worksheet' scope. Here is modified solution: public static IXLWorksheet CopyToWithNamedRanges(this IXLWorksheet sourceSheet, String targetSheetName) { if (sourceSheet == null) { throw new ArgumentNullException("Source Worksheet is Null!"); } IXLWorksheet targetWorksheet = sourceSheet.CopyTo(sourceSheet.Workbook, targetSheetName); foreach (IXLNamedRange namedRange in sourceSheet.Workbook.NamedRanges) { string[] splitAddress = namedRange.RefersTo.Split(new char[] { '!' }, 2); string worksheetName = splitAddress[0]; string newRange = targetWorksheet.Name + "!" + splitAddress[1]; targetWorksheet.NamedRanges.Add(namedRange.Name, newRange); } return targetWorksheet; } 1. Now we are reading named ranges from workbook which contains our source sheet. 2. We are not modifying original named range. 3. We are adding named ranges to target sheet, which automatically sets the scope to worksheet. 4. Original credit goes to Rynkadink.

New Post: item.Cell(10).ValueCached returns different data

$
0
0
item.Cell(10).Value=5.2323239773
item.Cell(10).ValueCached=5.2323239773+E2

why the above values are coming different.
Please let me know the exact use of ValueCached

Thanks
Razack
Viewing all 1877 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>