Click here to Skip to main content
14,733,443 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am exporting table from excel work book to pdf using itextsharp. I have table in excel workbook with almost 50 plus columns. I want to break that into multiple tables with each table having 10 columns only.

Below is code to export data from excel to pdf..

using (XLWorkbook workBook = new XLWorkbook(filePath.FullName))
{
    IXLWorksheet workSheet = workBook.Worksheet(1);
    bool firstRow = true;

        PdfPTable table = new PdfPTable(1);

        foreach (IXLRow row in workSheet.Rows())
        {
            if (firstRow)
            {
                table.ResetColumnCount(row.Cells().Count());
                table.WidthPercentage = 100;
                table.HeaderRows = 1;

                foreach (IXLCell cell in row.Cells())
                {
                    PdfPCell pdfcell = new PdfPCell(new Phrase(cell.Value.ToString()));

                    pdfcell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
                    pdfcell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
                    pdfcell.BackgroundColor = new iTextSharp.text.BaseColor(System.Drawing.Color.Orange);

                    table.AddCell(pdfcell);
                }
                firstRow = false;
            }
            else
            {
                foreach (IXLCell cell in row.Cells())
                {
                    PdfPCell pdfcell = new PdfPCell(new Phrase(cell.Value.ToString().Trim(), tableFont));

                    //Align the cell in the center
                    pdfcell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
                    pdfcell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;

                    table.AddCell(pdfcell);
                }
            }
        }
        pdfDoc.Add(table);
}


What I have tried:

I tried setting counters but it is not working as expected.
Posted
Updated 7-Jul-20 21:06pm
Comments
Garth J Lancaster 8-Jul-20 1:40am
   
How many rows are there in the worksheet ?

Splitting a single row into groups of 10 columns is 'easy', but you dont say how many rows you have, this may cause 'paging' issues as in the PDF Table running over the page.
Member 14636607 8-Jul-20 1:43am
   
Only one row

1 solution

ok, if row.Cells is enumerable in the LINQ sense, then
int chunkSize = 10; // Grouping Columns/cells into 'chunks'
var chunks = row.Cells().Select((x, i) => new { Index = i, Value = x })
.GroupBy(x => x.Index / chunkSize)
.Select(x => x.Select(v => v.Value).ToList())
.ToList();

Will produce a 'List of Lists' where
List 0 = List cells for Columns  1..10
List 1 = List cells for Columns 11..20
...
   
Comments
Maciej Los 8-Jul-20 2:20am
   
5ed!
Garth J Lancaster 8-Jul-20 4:43am
   
Thanks Maciej - I was looking more forward to maybe a skip and take paging solution for his rows, but it seems he only has one data row :-(
Maciej Los 8-Jul-20 5:32am
   
Seems that OP was looking for idea, how to split table by the number of columns. You gave Him/Her an idea... Adn this is the matter.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900