私は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;
}
が必要な場合は、次のようにheadding行を結合することを忘れていけません。 – StackTrace
これはiTextSharpの問題ではありません。 iTextSharpはあなたのためにそのチェックをしません。文字列「a」が文字列「b」と同一であるかどうかをチェックし、文字列「b」が文字列「c」と同一であるかどうかを確認することは問題ではない。何を試しましたか?なぜそれは動作しませんでしたか? –
私はブルーノに同意します。さらに、この段階でiTextSharpの使用を一時的に停止することをお勧めします。代わりに、 'DataTable'だけに注目してください。そのSQLシステムから来ている場合、あなたはそれを得るためにいくつかのSQLロジックを投げることができるかもしれません。 C#であれば、簡単なWinFormやASP.Net WebFormビューを 'DataTable'で作成し、必要なロジックを適用してください。いったん取得すれば、iTextSharpで作業するために投稿コードに簡単にスナップすることができます。 –