..我只想打印DataGrid中的数据.格式对齐,困扰好久了,大哥们给个方案.我试过好多方法.要么把整个页面全部表单输出.全乱了..要么就是输出内容后格式乱的厉害.无法打印..哪位大哥帮忙.帮我弄弄.怎么点按纽实现打印DataGrid中的数据.打印出来效果要好的!!! 急啊...

解决方案 »

  1.   

    导出EXCEL
    或者用报表工具做报表.
      

  2.   

    我是自己把数据写成一个HTML页
    再调用IE自带的打印工具打印
    效果还行..
      

  3.   

    <div id="con" runat="server">
    <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid><input type="hidden" id="content" runat="server"></div>
    ====
    cs代码:
    mailbody =content.Value.Replace("&it;","<").Replace("&gt;",">");
    Response.Write(mailbody);
    方法二:
    <script language="javascript">
    function preview()
    {
    bdhtml=window.document.body.innerHTML;
    sprnstr="<!--startprint-->";
    eprnstr="<!--endprint-->";
    prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
    prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
    window.document.body.innerHTML=prnhtml;
    window.print();
    }
    </script><form id="Form1" method="post" runat="server">
    <center>本部分以上不被打印</center>
    <!--startprint-->
    <div align="center">
    <asp:DataGrid id="dgShow" runat="server" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px"
    BackColor="White" CellPadding="4">
    <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
    <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
    <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
    <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
    </asp:DataGrid>
    </div>
    <!--endprint-->
    <center>本部分以下不被打印</center>
    <div align="center">
    <input type="button" name="print" value="预览并打印" onclick="preview()">
    </div>两个方法都可以,有什么不清楚的,可以给我留言,或qq:512965230
      

  4.   

    到ms去找一个叫ReportClass的开源的东东,专门打印datagrid的,比较好用
      

  5.   

    Private Sub excelDisplay()
            Dim excel As Excel.Application = New Excel.Application()
            Dim rowIndex As Integer = 1
            Dim colIndex As Integer = 0
            Dim table As DataTable = GetData()
            Dim col As DataColumn
            Dim row As DataRow
            'add workbook
            excel.Application.Workbooks.Add(True)
            excel.Cells(1, 1) = ArrayCommon(14).ToString
            excel.Cells(1, 2) = ArrayCommon(16).ToString
            excel.Cells(1, 3) = ArrayCommon(19).ToString
            excel.Cells(1, 4) = ArrayCommon(20).ToString
            'data exists
            If intCheckNothing = 1 Then
                'set activesheet
                With excel.ActiveSheet
                    'title font
                    .Range(.Cells(1, 1), .Cells(1, table.Columns.Count)).Font.Bold = True
                    .Range(.Cells(1, 1), .Cells(table.Rows.Count + 1, table.Columns.Count)).Borders.LineStyle = 1
                    .Range("a1", "a1").ColumnWidth = 13
                    .Range("b1", "b1").ColumnWidth = 25
                    .Range("c1", "c1").ColumnWidth = 22
                    .Range("d1", "d1").ColumnWidth = 22
                    .Range(.Cells(1, 1), .Cells(table.Rows.Count + 1, table.Columns.Count)).HorizontalAlignment = 3
                End With            For Each row In table.Rows
                    'fill in data
                    rowIndex = rowIndex + 1
                    colIndex = 0
                    For Each col In table.Columns
                        colIndex = colIndex + 1
                        excel.Cells(rowIndex, colIndex) = row(col.ColumnName).ToString()
                    Next
                Next
            Else
                'data doesn't exist
                excel.Cells.Font.Bold = True
                excel.Cells.Range("a1", "d1").Borders.LineStyle = 1
                excel.Cells.ColumnWidth = 20
                excel.Cells.HorizontalAlignment = 3
            End If
            excel.Visible = True
        End Sub
      

  6.   

    7楼好象是VB里面的那么写吧。。我在ASP。NET中没有直接的Excel对象。只能用这么写:
      // 当前对话 
    System.Web.HttpContext curContext = System.Web.HttpContext.Current; 
       // IO用于导出并返回excel文件 
    System.IO.StringWriter strWriter = null; 
    System.Web.UI.HtmlTextWriter htmlWriter = null; 

    if (dbg != null) 

    // 设置编码和附件格式 
    curContext.Response.ContentType = "application/vnd.ms-excel"; 
    curContext.Response.ContentEncoding =System.Text.Encoding.Default; 
    curContext.Response.Charset = "UTF-8"; 
    // 导出excel文件 
    strWriter = new System.IO.StringWriter(); 
    htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter); 
    // 返回客户端     
    dbg.RenderControl(htmlWriter);     
    curContext.Response.Write(strWriter.ToString()); 
    curContext.Response.End(); 
    dbg是方法传入的一个页面DataGrid  就是需要打印的那个  ,可是 导出Excel之后出现的结果是整个页面的表单信息全部出现。而且比较乱。。我只要打印DataGrid的数据。且可是要好。可是弄不好这样的效果。。谁能帮忙?