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

New Post: Read excel file located at web server

$
0
0
As the error said, URI are not supported :)

I believe you can use a WebRequest to the file URL, and use its getResponseStream method to have a valid Stream object to pass to the ClosedXML XLWorkbook object.
I have to tried it but I believe that might be a reasonable way to do that.

New Post: ClosedXML does not support Excel 2013

$
0
0
Hi All,

We have created a service using ClosedXML version 0.76. The feature it includes is to upload and download data in excel. The user is allowed to update minimum data, therefore the sheet is protected and only that data which is allowed to be updated is enabled.

This functionality works well using Excel 2010. However, it does not support in excel 2013.
The download is done successfully However, during an upload, when the code tries to unprotect the sheet using the password, it is not able to do so and breaks. However, when I open the Excel and unprotect the sheet with the same password it works fine.

Please suggest the next steps to resolve this issue asap.

Regards,
Geeta.

Created Unassigned: ClosedXML does not support Excel 2013 [9476]

$
0
0
Hi All,

We have created a service using ClosedXML version 0.76. The feature it includes is to upload and download data in excel. The user is allowed to update minimum data, therefore the sheet is protected and only that data which is allowed to be updated is enabled.

This functionality works well using Excel 2010. However, it does not support in excel 2013.
The download is done successfully However, during an upload, when the code tries to unprotect the sheet using the password, it is not able to do so and breaks. However, when I open the Excel and unprotect the sheet with the same password it works fine.

Please suggest the next steps to resolve this issue asap.

Regards,
Geeta.


Edited Unassigned: ClosedXML does not support Excel 2013 [9476]

$
0
0
Hi All,

We have created a service using ClosedXML version 0.76. The feature it includes is to upload and download data in excel. The user is allowed to update minimum data, therefore the sheet is protected and only that data which is allowed to be updated is enabled.

This functionality works well using Excel 2010. However, it does not support in excel 2013.
The download is done successfully However, during an upload, when the code tries to unprotect the sheet using the password, it is not able to do so and breaks. However, when I open the Excel and unprotect the sheet with the same password it works fine.

Confguration : Windows 7, Browser IE 11, Office 2013

Please suggest the next steps to resolve this issue asap.

Regards,
Geeta.


Created Unassigned: SaveAs() is saving my file in IIS Express by default, i would like to save using open dialog box.. how can i do this?? [9477]

$
0
0
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt, "BPA Supplier Report");
wb.SaveAs(@"C:\ME\Projects\Planogram");
}

New Post: Conditional formating of a named range

$
0
0
Need to apply conditional formatting to a named range or other range in which the cells or columns in the range are not contiguous.

Below a named range is built from each column in which the value includes the word "DoDAAC." These columns are not necessarily beside one another and I need to know if there are duplicate values in them. I am attempting to do that by creating a conditional format rule that will highlight dups throughout the columns that are not beside one another. Now I know I can do this several ways at creation of the sheet by collecting the values in a list, looking for dups and highlighting them. But the resulting spreadsheet is distributed and updated by users after creation. If they enter a duplicate value I want the cell to turn red.

If the below range "rg" is a contiguous range, no problem the conditional formatting works. However, if there are many ranges in "rg" like "I:I,K:K,M:M" which can be the case in a named range then only the first range has the formatting applied.


'this will work fine if the range "rg" is equal to a normal contiguous range
rg.AddConditionalFormat().WhenIsDuplicate().Fill.SetBackgroundColor(cxl.XLColor.BabyPink).Font.SetFontColor(cxl.XLColor.Red)


But if I create a non-contiguous range, like below, the formatting only gets applied to the 1st range in the list of ranges included in the named range.


'Create a named range in the worksheet with all the DoDAAC columns
    For Each C As cxl.IXLCell In .Row(1).CellsUsed
        If C.Value.ToString Like "*DoDAAC*" Or _
           C.Value.ToString Like "*DoDAAC*" Or _
           C.Value.ToString Like "*DoDAAC*" Then
            C.WorksheetColumn.AddToNamed("DoDAACs", ClosedXML.Excel.XLScope.Workbook)
        End If
    Next
'make a range object equl to the named range in the worksheet
Dim nr = wkb.Range("DoDAACs")

'Here I attempt to apply the formatting to the named range and it only get applied to the first range in the ranges of the named range.
nr.AddConditionalFormat().WhenIsDuplicate().Fill.SetBackgroundColor(cxl.XLColor.BabyPink).Font.SetFontColor(cxl.XLColor.Red)



It wont work to cycle thru all the ranges and apply the formatting separately as I'm checking for duplicate across all the columns.

Created Unassigned: Encoding problems [9478]

$
0
0
Hi,
I create a worksheet stored in MemoryStream then read as a string. I have to pass this string as base64 encoded via ajax reqest.
The problem is that i get back something like this "PK[QHó·f ¸xl/workbook.xml ˘( ŤMnÂ0…ŻbÍľ8D˘­"›nŘT•ŠÚµ±ÇÄ"¶#ŹÜ­‹©W¨ˆ@]uĺůűŢĽńĎ×÷r=¸Žť0’...."
I tried with default encoding and utf-8 encoding but not worked!
This is my code :
public string DataTableToXLS(DataTable sourceTable)
{

Stream fs = new MemoryStream();
var wb = new XLWorkbook();
wb.Worksheets.Add(sourceTable);
wb.SaveAs(fs);
fs.Position = 0;
var sr = new StreamReader(fs,Encoding.Default,false);
var myStr = sr.ReadToEnd();
return myStr;
}

Created Unassigned: Formula on date value not working [9479]

$
0
0
Hi,
I'm trying to import a file containig a formula on a date filed like this:
```
| A | B |
| 25/02/2016 | A1+1 |
```

However when I read the cell B1 the value type is double (I use this code)
```
item.Cell(y).Value.GetType().Name;
```

If I try reading the cell using this code
```
item.Cell(y).TryGetValue(out value)
```
I get this decimal number: 42426.0

Do you have any solution?
Thank you.


Created Unassigned: Protecting a Range is handled inefficiently [9480]

$
0
0
When applying cell protection to a Range, this is handled on a cell-by-cell basis. That is very inefficient for large ranges.

For example the following test produces an OutOfMemoryException on my machine. The test sets the first 6 columns of the sheet to be protected. The way ClosedXML does this is to enumerate each cell in the range, then set the protection style. And because each column is considered as a range with 1048576 cells, this requires a lot of processing.

When doing this in Excel or LibreOffice, it's fast and the resulting file is small. So apparently there is a way to do it more efficiently.

Possibly this issue applies to other types of styling as well (e.g. setting the background of a whole column), that should be tested further.

```
using ClosedXML.Excel;
using NUnit.Framework;

namespace ClosedXML_Tests
{
[TestFixture]
public class LargeRangeTests
{
[Test]
public void CanCreateLargeRange()
{
var wb = new XLWorkbook();
var ws1 = wb.Worksheets.Add("Sheet1");

var range = ws1.Range("A:F");

Assert.AreEqual(range.LastRow().RowNumber(), 1048576, "Row number of last row");

range.Style.Protection.Locked = true;

Assert.True(range.LastRow().Style.Protection.Locked, "Last row is locked");
}
}
}
```

Edited Unassigned: Styling a large Range is handled inefficiently [9480]

$
0
0
When applying cell protection to a Range, this is handled on a cell-by-cell basis. That is very inefficient for large ranges.

For example the following test produces an OutOfMemoryException on my machine. The test sets the first 6 columns of the sheet to be protected. The way ClosedXML does this is to enumerate each cell in the range, then set the protection style. And because each column is considered as a range with 1048576 cells, this requires a lot of processing.

When doing this in Excel or LibreOffice, it's fast and the resulting file is small. So apparently there is a way to do it more efficiently.

Possibly this issue applies to other types of styling as well (e.g. setting the background of a whole column), that should be tested further.

```
using ClosedXML.Excel;
using NUnit.Framework;

namespace ClosedXML_Tests
{
[TestFixture]
public class LargeRangeTests
{
[Test]
public void CanCreateLargeRange()
{
var wb = new XLWorkbook();
var ws1 = wb.Worksheets.Add("Sheet1");

var range = ws1.Range("A:F");

Assert.AreEqual(range.LastRow().RowNumber(), 1048576, "Row number of last row");

range.Style.Protection.Locked = true;

Assert.True(range.LastRow().Style.Protection.Locked, "Last row is locked");
}
}
}
```

Update: it applies to other forms of styling as well, like setting the background of the whole column. The test below also leads to an OutOfMemoryException.

```
[Test]
public void CanSetColumnsBackground()
{
var wb = new XLWorkbook();
var ws1 = wb.Worksheets.Add("Sheet1");

var range = ws1.Range("A:F");

Assert.AreEqual(range.LastRow().RowNumber(), 1048576, "Row number of last row");

range.Style.Fill.BackgroundColor = XLColor.RadicalRed;

Assert.AreEqual(range.LastRow().Style.Fill.BackgroundColor, XLColor.RadicalRed, "Last row is red");
}
```

Commented Unassigned: Styling a large Range is handled inefficiently [9480]

$
0
0
When applying cell protection to a Range, this is handled on a cell-by-cell basis. That is very inefficient for large ranges.

For example the following test produces an OutOfMemoryException on my machine. The test sets the first 6 columns of the sheet to be protected. The way ClosedXML does this is to enumerate each cell in the range, then set the protection style. And because each column is considered as a range with 1048576 cells, this requires a lot of processing.

When doing this in Excel or LibreOffice, it's fast and the resulting file is small. So apparently there is a way to do it more efficiently.

Possibly this issue applies to other types of styling as well (e.g. setting the background of a whole column), that should be tested further.

```
using ClosedXML.Excel;
using NUnit.Framework;

namespace ClosedXML_Tests
{
[TestFixture]
public class LargeRangeTests
{
[Test]
public void CanCreateLargeRange()
{
var wb = new XLWorkbook();
var ws1 = wb.Worksheets.Add("Sheet1");

var range = ws1.Range("A:F");

Assert.AreEqual(range.LastRow().RowNumber(), 1048576, "Row number of last row");

range.Style.Protection.Locked = true;

Assert.True(range.LastRow().Style.Protection.Locked, "Last row is locked");
}
}
}
```

Update: it applies to other forms of styling as well, like setting the background of the whole column. The test below also leads to an OutOfMemoryException.

```
[Test]
public void CanSetColumnsBackground()
{
var wb = new XLWorkbook();
var ws1 = wb.Worksheets.Add("Sheet1");

var range = ws1.Range("A:F");

Assert.AreEqual(range.LastRow().RowNumber(), 1048576, "Row number of last row");

range.Style.Fill.BackgroundColor = XLColor.RadicalRed;

Assert.AreEqual(range.LastRow().Style.Fill.BackgroundColor, XLColor.RadicalRed, "Last row is red");
}
```
Comments: For practical purposes, don't use Ranges when you want to style/protect a whole column. Like this it works just fine: ``` var columns = ws1.Columns(1, 6); columns.Style.Fill.BackgroundColor = XLColor.RadicalRed; ``` Basically the way I used ClosedXML triggered the inefficiency. Cases like I mentioned could be solved more efficiently (i.e. handling a range like "A:F" on column-by-column basis, not cell-by-cell), but I think for most practical purposes there is a good way to achieve the desired result by using the right functions. Complete tests added for posterity: ``` using ClosedXML.Excel; using NUnit.Framework; namespace ClosedXML_Tests { [TestFixture] public class LargeRangeTests { [Test] [ExpectedException(ExpectedException = typeof(System.OutOfMemoryException))] public void CanSetColumnsProtectedWithRange() { var wb = new XLWorkbook(); var ws1 = wb.Worksheets.Add("Sheet1"); var range = ws1.Range("A:F"); Assert.AreEqual(range.LastRow().RowNumber(), 1048576, "Row number of last row"); range.Style.Protection.Locked = true; Assert.True(range.LastRow().Style.Protection.Locked, "Last row is locked"); } [Test] [ExpectedException(ExpectedException = typeof(System.OutOfMemoryException))] public void CanSetColumnsBackgroundWithRange() { var wb = new XLWorkbook(); var ws1 = wb.Worksheets.Add("Sheet1"); var range = ws1.Range("A:F"); Assert.AreEqual(range.LastRow().RowNumber(), 1048576, "Row number of last row"); range.Style.Fill.BackgroundColor = XLColor.RadicalRed; Assert.AreEqual(range.LastRow().Style.Fill.BackgroundColor, XLColor.RadicalRed, "Last row is red"); } [Test] public void CanSetColumnsBackground() { var wb = new XLWorkbook(); var ws1 = wb.Worksheets.Add("Sheet1"); var columns = ws1.Columns(1, 6); columns.Style.Fill.BackgroundColor = XLColor.RadicalRed; Assert.AreEqual(ws1.Cell(1048576, 1).Style.Fill.BackgroundColor, XLColor.RadicalRed, "Last cell is red"); } } } ```

Edited Unassigned: Styling a large Range is handled inefficiently [9480]

$
0
0
__Update__: _If you want to style/protect a whole column or row, use .Columns(..) and .Rows(..), not .Range(..)._

When applying cell protection to a Range, this is handled on a cell-by-cell basis. That is very inefficient for large ranges.

For example the following test produces an OutOfMemoryException on my machine. The test sets the first 6 columns of the sheet to be protected. The way ClosedXML does this is to enumerate each cell in the range, then set the protection style. And because each column is considered as a range with 1048576 cells, this requires a lot of processing.

When doing this in Excel or LibreOffice, it's fast and the resulting file is small. So apparently there is a way to do it more efficiently.

Possibly this issue applies to other types of styling as well (e.g. setting the background of a whole column), that should be tested further.

```
using ClosedXML.Excel;
using NUnit.Framework;

namespace ClosedXML_Tests
{
[TestFixture]
public class LargeRangeTests
{
[Test]
public void CanCreateLargeRange()
{
var wb = new XLWorkbook();
var ws1 = wb.Worksheets.Add("Sheet1");

var range = ws1.Range("A:F");

Assert.AreEqual(range.LastRow().RowNumber(), 1048576, "Row number of last row");

range.Style.Protection.Locked = true;

Assert.True(range.LastRow().Style.Protection.Locked, "Last row is locked");
}
}
}
```

Update: it applies to other forms of styling as well, like setting the background of the whole column. The test below also leads to an OutOfMemoryException.

```
[Test]
public void CanSetColumnsBackground()
{
var wb = new XLWorkbook();
var ws1 = wb.Worksheets.Add("Sheet1");

var range = ws1.Range("A:F");

Assert.AreEqual(range.LastRow().RowNumber(), 1048576, "Row number of last row");

range.Style.Fill.BackgroundColor = XLColor.RadicalRed;

Assert.AreEqual(range.LastRow().Style.Fill.BackgroundColor, XLColor.RadicalRed, "Last row is red");
}
```

Created Unassigned: Excel header Gettext [9481]

$
0
0
The Excel header Gettext is not returning values in the correct occurrence. I am reading a Excel document and have the same header on all pages. workSheet.PageSetup.Header.GetText(XLHFOccurrence.AllPages) returned empty string. workSheet.PageSetup.Header.GetText(XLHFOccurrence.OddPages) returned the value I have on every page.

code example:
using (XLWorkbook workBook = new XLWorkbook(file))
{
foreach (IXLWorksheet workSheet in workBook.Worksheets)
{
string WorkSheetHeaders = string.Concat(
workSheet.PageSetup.Header.GetText(XLHFOccurrence.AllPages),
workSheet.PageSetup.Header.GetText(XLHFOccurrence.EvenPages),
workSheet.PageSetup.Header.GetText(XLHFOccurrence.FirstPage),
workSheet.PageSetup.Header.GetText(XLHFOccurrence.OddPages));


New Post: Create zip of workbooks and download

$
0
0
I'm trying to create several workbooks in memory and putting them into a zip file that will be returned to the browser for download. Combining the example here to deliver an XLWorkbook for download and using ZipArchive functionality in the newest versions of ASP, here's a snippet of what I've done:

using (var ZipStream = new MemoryStream())
            {
                using (var Archive = new ZipArchive(ZipStream, ZipArchiveMode.Create, true))
                {
                    for (int i = 0; i < ExperimentList.Count(); i++)
                    {

                        WorkbookFileName = "Experiment_" + ExperimentList[i].ExperimentID.ToString() + ".xlsx";
                        dt = GetData(ExperimentList[i].ExperimentID);
                        Workbook = GenerateExcelDoc(dt, WorkbookFileName, SheetName);

                        var File = Archive.CreateEntry(WorkbookFileName);

                        using (var entryStream = File.Open())
                        using (var streamWriter = new StreamWriter(entryStream))
                        {
                            Workbook.SaveAs(ZipStream);
                            ZipStream.Seek(0, SeekOrigin.Begin);
                        }
                    }
                }

                // create HTTP Response
                HttpResponse httpResponse = System.Web.HttpContext.Current.Response;
                httpResponse.Clear();
                httpResponse.ContentType = "application/zip";
                httpResponse.AddHeader("content-disposition", "attachment; filename=" + ZipFileName);

                // deliver zip file from memory stream to browser
                //ZipStream.Seek(0, SeekOrigin.Begin);
                ZipStream.WriteTo(httpResponse.OutputStream);
                ZipStream.Seek(0, SeekOrigin.Begin);
                ZipStream.Close();
                httpResponse.End();
            }
When I open the zip file, I don't see any workbooks. Instead I get what I believe are components to make an XLWorkbook. Here's the top directory in the zip:
  • _rels/
  • docProps/
  • package/
  • xl/
    [Content_Types].xml
No matter how many files I attempt to zip, it's just this basic directory structure (with more files in each folder). The total zip file always comes out about 17 kb.

I've also tried other variations in my code like using

streamWriter.Write(workbook);

instead of

Workbook.SaveAs(ZipStream);

and I end up getting a zip file w/ all the workbooks I wanted. But I can't open any of the workbooks and instead I get a 'file corrupted' message.

Has anyone else been able to write and zip several Excel docs in memory and delivered directly to the browser for download?

Reviewed: ClosedXML 0.76.0 (Mar 08, 2016)

$
0
0
Rated 5 Stars (out of 5) - After I have lot of struggle with Response write method to create excel, now this comes to me as Piece of Cake. This is my life saver from now onwards.

New Post: How to download the generated file?

$
0
0
How to download the generated file and let the user to save it at any location?

New Comment on "Adding DataTable as Worksheet"

$
0
0
Does anyone know how produce the plain excel i.e. without filter and background color? I tried to remove the header background color, border color but not able to do it.

Commented Issue: transparent color [6398]

$
0
0
Hi
 
how i can set the backgroundcolor of a cell to a transparent color?
Comments: How can i produce the plain excel i.e. no filter and background color?

Created Unassigned: Datatype "DateTime" not set for emtpy cells [9482]

$
0
0
Hello,

I am setting the datatype for a range of cells to "DateTime":
```
rangeTable.DataRange.Column(colNumber).SetDataType(XLCellValues.DateTime);
```
where rangeTable is an IXLTable and colNumber an int.

This sets the datatype for all cells that have a value, but it does not set it for empty cells. I need it for all cells.
By the way, setting the datatype to "XLCellValues.Number" works for all, empty and non-empty cells.

It would be great if someone could have a look at that.
Thank you!

Created Unassigned: Multiple SaveAs calls causes InvalidCastException [9483]

$
0
0
Version 0.76.0, Test case:

```
XLWorkbook w = new XLWorkbook();
IXLWorksheet ret = w.Worksheets.Add("test");
w.SaveAs("test.xlsx");
ret.Cell("A1").Value = "test";
w.SaveAs("test"xlsx");
```

The first save succedes, the second one fails with error:

System.InvalidCastException: Unable to cast object of type 'DocumentFormat.OpenXml.Packaging.SharedStringTablePart' to type 'DocumentFormat.OpenXml.Packaging.WorksheetPart'.

Workaround:
```
XLWorkbook w = new XLWorkbook();
IXLWorksheet ret = w.Worksheets.Add("test");
w.SaveAs(test.xlsx);
w = new XLWorkbook(test.xlsx);
w.Worksheets.Worksheet("test").Cell("A1").SetValue<string>("test");
w.Save();
w.Worksheets.Worksheet("test").Cell("A1").SetValue<string>("test2");
w.Save();
```

I also tried Save() after first SaveAs() but it says that I cannot use "Save" for new files.
Viewing all 1877 articles
Browse latest View live


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