2015-10-22 18 views
1

我有一個問題:如何使用iTextSharp在ASP.NET和C#中以PDF格式合併具有相同值的行?

我需要與此表相同的值合併行:

Table I need

此表是在GridView中,我認識到,合併該行的方法

private void AgruparGridView(GridViewRowCollection rows, int indiceInicial, int totalCeldasAgrupar) 
{ 
    //Si no hay celdas a agrupar no realiza ninguna acción 
    if (totalCeldasAgrupar == 0) 
     return; 
    int i, count = 1; 
    ArrayList lst = new ArrayList(); 
    // Los elementos del gridview son llenados en la lista. 
    lst.Add(rows[0]); 
    var ctrl = rows[0].Cells[indiceInicial]; 
    //Recorrer los registros que se tengan para agrupar 
    for (i = 1; i < rows.Count; i++) 
    { 
     TableCell nextCell = rows[i].Cells[indiceInicial]; 
     if (ctrl.Text == nextCell.Text) 
     { 
      count++; 
      nextCell.Visible = false; 
      lst.Add(rows[i]); 
     } 
     else 
     { 
      if (count > 1) 
      { 
       ctrl.RowSpan = count; 
       AgruparGridView(new GridViewRowCollection(lst), indiceInicial + 1, totalCeldasAgrupar - 1); 
      } 
      count = 1; 
      lst.Clear(); 
      ctrl = rows[i].Cells[indiceInicial]; 
      lst.Add(rows[i]); 
     } 
    } 
    if (count > 1) 
    { 
     ctrl.RowSpan = count; 
     AgruparGridView(new GridViewRowCollection(lst), indiceInicial + 1, totalCeldasAgrupar - 1); 
    } 
    count = 1; 
    lst.Clear(); 
} 

我需要在iTextSharp的PDF報告上實現相同的結果,但我不知道這個過程。

是構建本節中的代碼片段是這樣的:

public PdfPTable Tablas(int TipoSeccion, int TipoDoc, int ClaveEntidad, int ClaveSubSis, string descripcion) 
    { 
     PDFEvents oPDFEvents = new PDFEvents(); 
     string sError = string.Empty; 
     int columns = 1; 
     int i = 0; 
     DataSet _DatosTabla = null; 
     PdfPTable Table; 
     PdfPCell CeldaNoBorde = new PdfPCell(CeldasSinBorde("", 1, 1, 4, 255, 255, 255)); 
     #region PerfilesDOCENTE 
     #region Tipo3 
     if (TipoSeccion == 3) 
     { 

      columns = 3; 

      Table = new PdfPTable(columns); 
      Table.WidthPercentage = 80; 
      float[] widhts = new float[] { 15, 50 }; 
      //Header Superior 
      if (this.TipoSeccionOriginal == 3 || this.TipoSeccionOriginal==0) 
      { 

       Table.AddCell(Celdas("ASIGNATURAS POR EXAMEN DISCIPLINAR PARA FUNCIONES DOCENTES", 2, 3, 8, 238, 233, 233)); 
        Table.AddCell(Celdas("Área disciplinar", 3, 1, 10, 238, 233, 233)); 
        Table.AddCell(Celdas("Asignatura", 3, 1, 10, 238, 233, 233)); 
        Table.AddCell(Celdas("Evaluación Disciplinar", 3, 1, 10, 238, 233, 233)); 
        _DatosTabla = DameDatosTablaPerfilesTelebach(ClaveEntidad, ClaveSubSis); 
      } 
      else 
      { 
       Table.AddCell(Celdas("ASIGNATURAS A CONCURSO PARA FUNCIONES TÉCNICO DOCENTES", 1, 2, 8, 238, 233, 233)); 
       Table.AddCell(Celdas("Evaluación", 2, 1, 10, 238, 233, 233)); 
       Table.AddCell(Celdas("Asignaturas", 2, 1, 10, 238, 233, 233)); 
       _DatosTabla = DameDatosTablaPerfiles(TipoDoc, ClaveEntidad, ClaveSubSis, true); 
      } 

      if (_DatosTabla != null) 
      { 
       while (i < _DatosTabla.Tables[0].Rows.Count) 
        { 
         Table.AddCell(oPDFEvents.FontPhrase(_DatosTabla.Tables[0].Rows[i][0].ToString(), 10)); 
         Table.AddCell(oPDFEvents.FontPhrase(_DatosTabla.Tables[0].Rows[i][1].ToString(), 10)); 
         Table.AddCell(oPDFEvents.FontPhrase(_DatosTabla.Tables[0].Rows[i][2].ToString(), 10)); 
         i = i + 1; 
        } 
      } 
      return Table; 
     } 

我的結果是這樣的

My actual result

對不起我的英語不好! 謝謝您的關注!

+0

當你說*使用相同的值*合併行,你的意思合併來自不同行的細胞轉化爲1個細胞,即一個*行跨度*?在屏幕截圖中,帶有文本「*Matemáticasy Ciencias Experimientales *」的單元格的行跨度爲14. – rhens

+0

是的,屏幕截圖是在aspx中,但是我不知道如何執行此操作以導出爲PDF使用iTextSharp,'因爲這是在我的工作中以pdf格式導出的工具 –

+1

@MarcoLuna上面發佈的代碼僅用於合併GridView控件的行。您需要發佈與iTextSharp相關的代碼。如何將行導出爲PDF,到目前爲止,您在iTextSharp中如何實現rowspan? – haraman

回答

1

您需要在適當的PdfPCell上設置Rowspan = n,並省略在該列中添加下一個n-1單元格。這是一個首先構建常規3x3表的示例。單元格標記爲行,列爲。然後在第一個單元格上使用Rowspan = 3構建表格。不添加第四個(2,1)和第七個(3,1)單元。

Document doc = new Document(); 
PdfWriter writer = 
    PdfWriter.GetInstance(doc, new FileStream("tables.pdf", FileMode.Create)); 
doc.Open(); 
doc.Add(new Paragraph("Table without rowspan:")); 
PdfPTable table = new PdfPTable(3); 
table.SpacingBefore = 10; 
table.AddCell(new PdfPCell(new Phrase("1,1"))); 
table.AddCell(new PdfPCell(new Phrase("1,2"))); 
table.AddCell(new PdfPCell(new Phrase("1,3"))); 
table.AddCell(new PdfPCell(new Phrase("2,1"))); 
table.AddCell(new PdfPCell(new Phrase("2,2"))); 
table.AddCell(new PdfPCell(new Phrase("2,3"))); 
table.AddCell(new PdfPCell(new Phrase("3,1"))); 
table.AddCell(new PdfPCell(new Phrase("3,2"))); 
table.AddCell(new PdfPCell(new Phrase("3,3"))); 
doc.Add(table); 
doc.Add(new Paragraph("Table with rowspan 3 on first cell:")); 
PdfPTable tableWithRowspan = new PdfPTable(3); 
tableWithRowspan.SpacingBefore = 10; 
PdfPCell cellWithRowspan = new PdfPCell(new Phrase("1,1")); 
// The first cell spans 3 rows 
cellWithRowspan.Rowspan = 3; 
tableWithRowspan.AddCell(cellWithRowspan); 
tableWithRowspan.AddCell(new PdfPCell(new Phrase("1,2"))); 
tableWithRowspan.AddCell(new PdfPCell(new Phrase("1,3"))); 
// Cell 2,1 does not exist 
tableWithRowspan.AddCell(new PdfPCell(new Phrase("2,2"))); 
tableWithRowspan.AddCell(new PdfPCell(new Phrase("2,3"))); 
// Cell 3,1 does not exist 
tableWithRowspan.AddCell(new PdfPCell(new Phrase("3,2"))); 
tableWithRowspan.AddCell(new PdfPCell(new Phrase("3,3"))); 
doc.Add(tableWithRowspan); 
doc.Close(); 

結果:

Table with rowspan

+0

我正在實現與上面相同的概念,但所有rowspan行直接來自數據庫,所以如果我們正在通過數據庫,我們是否可以達到同樣的目標。 – Manjuboyz

相關問題