2016-04-12 5 views
0

私はitextSharpを使用して、DataTableをpdfテーブルにエクスポートしています。私は、以下に投稿したサンプルコードを使用してデータをpdfテーブルにエクスポートできます。 DataTableには21個の列があります。PDFの同じ値を持つ行の最初の3列をマージする方法

pdf(DataTable)の最初の列には、任意の数の行について同様の値が含まれている場合があります。行のグループの最初の列のデータ値が似ている場合、それらの行の最初の3つの列を1つのセルとして結合します。

これを達成するために、以下のコードを変更する際に問題があります。

public iTextSharp.text.Table GetItextTable(DataTable dtChartData, string reportType) 
    { 
     int intCols = dtChartData.Columns.Count; //Total number of columns 
     int intRows = dtChartData.Rows.Count; //Total number of rows 
     iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(intCols, intRows); 
     try 
     { 
      pdfTable.BorderWidth = 1; 
      pdfTable.Width = 100; 
      pdfTable.Padding = 1; 
      pdfTable.Spacing = 1; 
      /*creating table headers */ 
      for (int i = 0; i < intCols; i++) 
      { 

       iTextSharp.text.Cell cellCols = new iTextSharp.text.Cell(); 
       iTextSharp.text.Font ColFont = iTextSharp.text.FontFactory.GetFont("Tahoma", 07, 
        iTextSharp.text.Font.BOLD); 
       for (int l = 0; l < dtChartData.Columns.Count; l++) 
       { 
        if (dtChartData.Columns[l].ColumnName.Contains("_")) 
        { 
         dtChartData.Columns[l].ColumnName = dtChartData.Columns[l].ColumnName.Replace("_", " "); 
        } 
       } 
       iTextSharp.text.Chunk chunkCols = new iTextSharp.text.Chunk(dtChartData.Columns[i].ColumnName,ColFont); 
       cellCols.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER; 
       if ((chunkCols.ToString().ToLower() == "ReportDetails")) 
       { 
        cellCols.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER; 
       } 
      } 
      /* loop that take values from every row in datatable and insert in itextsharp table */ 
      for (int k = 0; k < intRows; k++) 
      { 
       for (int j = 0; j < intCols; j++) 
       { 
        iTextSharp.text.Cell cellRows = new iTextSharp.text.Cell(); 
        iTextSharp.text.Font RowFont = iTextSharp.text.FontFactory.GetFont("Tahoma", 07); 
        iTextSharp.text.Chunk chunkRows = new iTextSharp.text.Chunk(dtChartData.Rows[k][j].ToString(),RowFont); 

        cellRows.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER; 
        cellRows.Add(chunkRows); 
        pdfTable.AddCell(cellRows); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      //error handling code here removed 
     } 
     return pdfTable; 
    } 

答えて

1

Colspanを変更しようとしましたか?この場合

iTextSharp.text.Cell cell = new iTextSharp.text.Cell(); 
cell.AddElement(new Paragraph("colspan 3")); 
cell.Colspan = 3; 
table.AddCell(cell); 

cellは、3つの列にまたがるます。

+0

が必要な場合は、次のようにheadding行を結合することを忘れていけません。 – StackTrace

+0

これはiTextSharpの問題ではありません。 iTextSharpはあなたのためにそのチェックをしません。文字列「a」が文字列「b」と同一であるかどうかをチェックし、文字列「b」が文字列「c」と同一であるかどうかを確認することは問題ではない。何を試しましたか?なぜそれは動作しませんでしたか? –

+1

私はブルーノに同意します。さらに、この段階でiTextSharpの使用を一時的に停止することをお勧めします。代わりに、 'DataTable'だけに注目してください。そのSQLシステムから来ている場合、あなたはそれを得るためにいくつかのSQLロジックを投げることができるかもしれません。 C#であれば、簡単なWinFormやASP.Net WebFormビューを 'DataTable'で作成し、必要なロジックを適用してください。いったん取得すれば、iTextSharpで作業するために投稿コードに簡単にスナップすることができます。 –

0

これを試してください。私はちょうどあなたのコードを編集しました。

ここで私はあなたのコードの1行を編集し、新しい行を追加しました(合計2行の変更のみ)。

あなたは、私が抱えている悩みは、行の最初の列が同じ値を持っているかどうかをチェックするためのロジックである

/* loop that take values from every row in datatable and insert in itextsharp table */ 
     for (int k = 0; k < intRows; k++) 
     { 
      for (int j = 2; j < intCols; j++)// EDIT: if all first 3 cols are same, then starts with 2 
      { 
       iTextSharp.text.Cell cellRows = new iTextSharp.text.Cell(); 
       if(j == 2) cellRows.Colspan = 3;// ADD: it'll gives a 3 times long cell 
       iTextSharp.text.Font RowFont = iTextSharp.text.FontFactory.GetFont("Tahoma", 07); 
       iTextSharp.text.Chunk chunkRows = new iTextSharp.text.Chunk(dtChartData.Rows[k][j].ToString(),RowFont); 

       cellRows.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER; 
       cellRows.Add(chunkRows); 
       pdfTable.AddCell(cellRows); 
      } 
     } 
関連する問題