Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting needs manual action to activate #707

Closed
1 of 2 tasks
VBartilucci opened this issue Feb 28, 2018 · 4 comments
Closed
1 of 2 tasks

Formatting needs manual action to activate #707

VBartilucci opened this issue Feb 28, 2018 · 4 comments
Labels

Comments

@VBartilucci
Copy link

VBartilucci commented Feb 28, 2018

Read and complete the full issue template

Do you want to request a feature or report a bug?

  • Bug
  • Feature

Version of ClosedXML

e.g. 0.87.1

What is the expected behavior or new feature?

Formatting set in ClosedXML does not correctly format data in the final document unless one uses text to columns or some other action on the data

Did this work in previous versions of our tool? Which versions?

Unknown

Reproducibility

Sample code for generating sheet from data table

    var wb = new XLWorkbook();
    wb.Worksheets.Add(ds);
    foreach (IXLWorksheet ws in wb.Worksheets)
    {
        if (value == 1)
        {
            ws.Column(16).Style.DateFormat.Format = "yyyy-MM-dd";
            ws.Column(17).Style.DateFormat.Format = "yyyy-MM-dd";
        }
        else
        { }
    }

    return wb;

The document has the formatting - you can check the columns in format data and it's there.

This is the same behavior as adding formatting to existing data in a sheet in Excel, so there's a chance there's no fix, save for the manual "tapping" I've been able to do. But a command to apply the formatting would be of use if it's not there.

@igitur
Copy link
Member

igitur commented Feb 28, 2018

I can't run your code sample.

@VBartilucci
Copy link
Author

It was included more to show the process - adding worksheets through data sets as opposed to manually, and adding the formatting after the add. If there's no process to force the sheet to recognize the formatting changes, the code is rather moot.

@igitur
Copy link
Member

igitur commented Mar 1, 2018

If you can't give me code that I can run to reproduce the issue, then there's nothing I can do about it.

@igitur
Copy link
Member

igitur commented Mar 2, 2018

This works perfectly on my machine.

using ClosedXML.Excel;
using System;
using System.Data;
using System.Linq;

public class Program
{
    public static void Main()
    {
        using (var wb = new XLWorkbook())
        {
            var dataSet = GetDataSet();

            // Add all DataTables in the DataSet as a worksheets
            wb.Worksheets.Add(dataSet);

            var ws = wb.Worksheets.First();

            ws.Column(4).Style.DateFormat.Format = "d MMMM yyyy";

            wb.SaveAs("outputfile.xlsx");
        }
    }

    private static DataSet GetDataSet()
    {
        var ds = new DataSet();
        ds.Tables.Add(GetTable("Patients"));
        ds.Tables.Add(GetTable("Employees"));
        ds.Tables.Add(GetTable("Information"));
        return ds;
    }

    private static DataTable GetTable(String tableName)
    {
        DataTable table = new DataTable();
        table.TableName = tableName;
        table.Columns.Add("Dosage", typeof(int));
        table.Columns.Add("Drug", typeof(string));
        table.Columns.Add("Patient", typeof(string));
        table.Columns.Add("Date", typeof(DateTime));

        table.Rows.Add(25, "Indocin", "David", new DateTime(2000, 1, 1));
        table.Rows.Add(50, "Enebrel", "Sam", new DateTime(2000, 1, 2));
        table.Rows.Add(10, "Hydralazine", "Christoff", new DateTime(2000, 1, 3));
        table.Rows.Add(21, "Combivent", "Janet", new DateTime(2000, 1, 4));
        table.Rows.Add(100, "Dilantin", "Melanie", new DateTime(2000, 1, 5));
        return table;
    }
}

@igitur igitur added the invalid label Mar 2, 2018
@igitur igitur closed this as completed Mar 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants