How do you style a row or cell based on value?

Technical support for Spire.DataExport

Moderators: iceblue support, DataExport, Manager

How do you style a row or cell based on value?

Postby will.marriott » Mon Oct 28, 2013 12:52 am

Hi, I'm looking to move from DevXpress to Spire.DataExport but your either lacking documentation or features. I want to export a DataTable but I want the font color to change based on the cell value e.g. negative values are red and positive is green. I want this then applied to all applicable exports e.g. PDF, HTML and XLSX. Is this possible?
will.marriott
 
Posts: 27
Joined: Thu Oct 24, 2013 9:47 pm

Re: How do you style a row or cell based on value?

Postby amy.zhao » Mon Oct 28, 2013 6:25 am

Hello,

Thanks for your interested in our products.
Sorry that our Spire.DataExport doesn't support to format the specific value at present, but our other products can achieve it.
You could please use Spire.Xls to get Excel.
Code: Select all
        static void GetExcel()
        {
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];
            //the datatable is yours
            sheet.InsertDataTable(datable, true, 1, 1);
            foreach (CellRange cell in sheet.Cells)
            {
                if (cell.Value == "negative")
                {
                    cell.Style.Font.Color = Color.Red;
                }
                if (cell.Value == "positive")
                {
                    cell.Style.Font.Color = Color.Green;
                }
            }
            workbook.SaveToFile("sample.xlsx", ExcelVersion.Version2007);
        }

You could please use Spire.Doc to get HTML and PDF you want.
Code: Select all
            Document document = new Document();
            Section section = document.AddSection();
            Table table= section.AddTable(true);
            table.TableFormat.Borders.BorderType = BorderStyle.Single;

            //the datatable is yours
            table.ResetCells(datatable.Rows.Count, datatable.Columns.Count);
            string value=null;

            TableCell cell=null;
            TextRange range = null;

            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                for (int j = 0; j < datatable.Columns.Count; j++)
                {
                     value= datatable.Rows[i][j].ToString();
                     cell = table.Rows[i].Cells[j];
                     cell.CellFormat.Borders.LineWidth = 1;
                     range= cell.AddParagraph().AppendText(value);

                    if (value == "negative")
                    {
                        range.CharacterFormat.TextColor = Color.Red;
                    }

                    if (value == "positive")
                    {
                        range.CharacterFormat.TextColor = Color.Green;
                    }
                }
            }
           
            //Get Html
           document.SaveToFile("sample.html",FileFormat.Html);
            //Get PDF
           //document.SaveToFile("sample.pdf",FileFormat.PDF);


Welcome to download and test Spire.Xls and Spire.Doc.
Downloading link of Spire.Xls: http://www.e-iceblue.com/Download/downl ... t-now.html
Downloading link of Spire.Doc: http://www.e-iceblue.com/Download/downl ... t-now.html

Please feel free to write to us if you have further problems.

Best regards,
Amy
E-iceblue support team
User avatar
amy.zhao
 
Posts: 2202
Joined: Wed Jun 27, 2012 8:50 am

Re: How do you style a row or cell based on value?

Postby amy.zhao » Tue Oct 29, 2013 3:49 am

Hello,

To let you better check the function, we sent you the full demos via e-mail because the attachment has 2MB maximum limition in the forum.

Please kindly check your e-mail. Please don't hesitate to contact us if you have further problems.

Best regards,
Amy
E-iceblue support team
User avatar
amy.zhao
 
Posts: 2202
Joined: Wed Jun 27, 2012 8:50 am


Return to Spire.DataExport

Who is online

Users browsing this forum: No registered users and 0 guests

cron