Click here to Skip to main content
15,072,311 members
Please Sign up or sign in to vote.
1.00/5 (3 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. The 50plus column table has one header row and one row with value...totally two rows.

Below is code to export data from excel to pdf...
C#
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:

C#
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();


but above code will have only header rows.
Posted
Updated 12-Jul-20 3:40am
v3
Comments
Richard MacCutchan 12-Jul-20 9:40am
   
What is the problem?
Laxmidhar tatwa technologies 13-Jul-20 11:40am
   
PdfPTable table = new PdfPTable(10); // 10 is the number of column

1 solution

Quote:
but above code will have only header rows
why ? you need to re-do your data processing loops, that's all.

On the first loop (the header row) ie 'firstRow', you store the result of this split/chunk for the headers.

You then get the data row, you said there was only one, yes ? so you apply this function to your data row.

You then loop through the data row 'split', for each iteration, get the split by index for the header row, build the table header, then process the data for that 'split'/chunk to add the relevant split/chunks for that set of columns

Don't be such a help vampire
   
v2
Comments
Member 14636607 12-Jul-20 9:46am
   
Expecting something better than that.
BillWoodruff 12-Jul-20 16:59pm
   
+5
Maciej Los 13-Jul-20 9:57am
   
5ed!

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