将数据绑定到datagrid上后,如何把datagrid内容打印出来
如:输出到用excel、word的形式打印出来,
要求不高,只要按照datagrid的样式打印就可以,且能够分页
有vb代码的高手提供一下,谢谢了!

解决方案 »

  1.   

    datagrid导出到excel并打印的vb原码谁有?
    问题解决立刻结贴。
      

  2.   

    Excel.Workbook myBook=myExcel.Workbooks[1];
    Excel.Sheets sheets=myBook.Worksheets;
    Excel.Worksheet mySheet=(Excel.Worksheet)sheets["dd"];
    mySheet.Activate(); int xrow=6;
    mySheet.Cells [6,1]=dataset4.Tables ["dj"].Rows [0]["项目名称"].ToString ();
    xrow=xrow+1;
    int signid=0;
    for (int xk=0;xk<dataset4.Tables ["dj1"].Rows.Count ;xk++)
    {
    if (xk==0)
    { mySheet.Cells [1,7]=strgcmc+"——投标文件";

    mySheet.Cells [2,7]="工程编号:"+strgcbh;
    }
    else
    {
    mySheet.Cells [xrow-6,7]=strgcmc+"—";
    mySheet.Cells [xrow-5,7]="工程编号:"+strgcbh;
    mySheet.Cells [xrow-1,1]=dataset4.Tables ["dj1"].Rows [xk]["项目名称"].ToString ();
    }
    int snn=1;
    for (int xi=signid;xi<dataset4.Tables ["dj"].Rows.Count ;xi++)
    {
    if (dataset4.Tables ["dj1"].Rows [xk]["dd"].ToString ()==dataset4.Tables ["dj"].Rows [xi]["dd"].ToString ())
    {
    int xcol=6; for (int xj=1;xj<7;xj++)
    {
    mySheet.Cells [xrow,4]=dataset4.Tables ["dj"].Rows [xi]["项目名称"].ToString ();
    mySheet.Cells [xrow,xcol]=dataset4.Tables ["dj"].Rows [xi][xj].ToString ();
    xcol=xcol+1;
    }
    xrow=xrow+1;
    signid=signid+1;
    }
    else
    {
    break;
    }
    snn=snn+1;
    }
    if (snn-23<0)
    {
    xrow=xrow+Math.Abs(snn-23);
    }
    xrow=xrow+19;
    }
      

  3.   

    http://community.csdn.net/expert/topic/2961/2961026.xml?temp=.6206324
      

  4.   

    http://community.csdn.net/Expert/topic/3340/3340036.xml?temp=.1504938
      

  5.   

    http://community.csdn.net/Expert/topic/3204/3204925.xml?temp=.2058985
      

  6.   

    我有从Dataset到Excel的代码,没有从DataGride的代码,是不是你需要的呢?
      

  7.   

    QPTYDL(QP):可以,“从 Dataset到Excel的代码” 也可以实现我的东西,能不能给一份你的代码,谢谢!
      

  8.   

    建議做成水晶報表,因為水晶報表即可以尋出為PDF,又可以導出為Word 和Excel ....還要以直接打印,好處多多..
      

  9.   

    有一段代码,但它是打开一个excel文件,如何能不打开而直接打印呢?请大家帮忙。1.先创建一个DataTable,作为数据来源,也可以另将其它的数据源。    Private Function CreaTable() As DataTable        Dim dt As New DataTable()        dt.Columns.Add("列1", GetType(String))        dt.Columns.Add("列2", GetType(Integer))        dt.Columns.Add("列3", GetType(String))        dt.Columns.Add("列4", GetType(String))        Dim row, row1 As DataRow        row = dt.NewRow()        row!列1 = "行1"        row!列2 = 1        row!列3 = "d"        row!列4 = "a"        dt.Rows.Add(row)        row1 = dt.NewRow()        row1!列1 = "行2"        row1!列2 = 12        row1!列3 = "b"        row1!列4 = "c"        dt.Rows.Add(row1)        Return dt    End Function2.将表中的内容导出到Excel                Dim xlApp As New Excel.Application()                Dim xlBook As Excel.Workbook                Dim xlSheet As Excel.Worksheet                 Dim rowIndex, colIndex As Integer                rowIndex = 1                colIndex = 0                 xlBook = xlApp.Workbooks().Add                xlSheet = xlBook.Worksheets("sheet1")                 Dim Table As New DataTable()                Table = CreaTable()                 '将所得到的表的列名,赋值给单元格                Dim Col As DataColumn                Dim Row As DataRow                For Each Col In Table.Columns                    colIndex = colIndex + 1                    xlApp.Cells(1, colIndex) = Col.ColumnName                Next                 '得到的表所有行,赋值给单元格                For Each Row In Table.Rows                    rowIndex = rowIndex + 1                    colIndex = 0                    For Each Col In Table.Columns                        colIndex = colIndex + 1                        xlApp.Cells(rowIndex, colIndex) = Row(Col.ColumnName)                    Next                Next                 With xlSheet                    .Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Name = "黑体"                    '设标题为黑体字                    .Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Bold = True                    '标题字体加粗                    .Range(.Cells(1, 1), .Cells(rowIndex, colIndex)).Borders.LineStyle = 1                    '设表格边框样式                End With                 With xlSheet.PageSetup                    .LeftHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10公司名称:"   ' & Gsmc                    .CenterHeader = "&""楷体_GB2312,常规""公司人员情况表&""宋体,常规""" & Chr(10) & "&""楷体_GB2312,常规""&10日 期:"                    .RightHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10单位:"                    .LeftFooter = "&""楷体_GB2312,常规""&10制表人:"                    .CenterFooter = "&""楷体_GB2312,常规""&10制表日期:"                    .RightFooter = "&""楷体_GB2312,常规""&10第&P页 共&N页"                End With                 xlApp.Visible = True
      

  10.   

    如有一个现成的代码,是老外写的C#,就向一个报表预览一样的东西,把datagrid的做成一个预览的画面,用户可以自已定义表头和样式,如果大家要,我可以给你们,请到http://www.01591.com的论坛里面留言
      

  11.   

    大家伙帮看一看下面这段代码,我把第一段去掉,把第二段的数据源换成dataset后,可以打印,但问题:1、打开excel是在服务器上,而不是在客户端,能不能在客户端打开excel?
        2、能不能控制一列的宽度,并使其自动换行?
    1.先创建一个DataTable,作为数据来源,也可以另将其它的数据源。    Private Function CreaTable() As DataTable        Dim dt As New DataTable()        dt.Columns.Add("列1", GetType(String))        dt.Columns.Add("列2", GetType(Integer))        dt.Columns.Add("列3", GetType(String))        dt.Columns.Add("列4", GetType(String))        Dim row, row1 As DataRow        row = dt.NewRow()        row!列1 = "行1"        row!列2 = 1        row!列3 = "d"        row!列4 = "a"        dt.Rows.Add(row)        row1 = dt.NewRow()        row1!列1 = "行2"        row1!列2 = 12        row1!列3 = "b"        row1!列4 = "c"        dt.Rows.Add(row1)        Return dt    End Function2.将表中的内容导出到Excel                Dim xlApp As New Excel.Application()                Dim xlBook As Excel.Workbook                Dim xlSheet As Excel.Worksheet                 Dim rowIndex, colIndex As Integer                rowIndex = 1                colIndex = 0                 xlBook = xlApp.Workbooks().Add                xlSheet = xlBook.Worksheets("sheet1")                 Dim Table As New DataTable()                Table = CreaTable()                 '将所得到的表的列名,赋值给单元格                Dim Col As DataColumn                Dim Row As DataRow                For Each Col In Table.Columns                    colIndex = colIndex + 1                    xlApp.Cells(1, colIndex) = Col.ColumnName                Next                 '得到的表所有行,赋值给单元格                For Each Row In Table.Rows                    rowIndex = rowIndex + 1                    colIndex = 0                    For Each Col In Table.Columns                        colIndex = colIndex + 1                        xlApp.Cells(rowIndex, colIndex) = Row(Col.ColumnName)                    Next                Next                 With xlSheet                    .Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Name = "黑体"                    '设标题为黑体字                    .Range(.Cells(1, 1), .Cells(1, colIndex)).Font.Bold = True                    '标题字体加粗                    .Range(.Cells(1, 1), .Cells(rowIndex, colIndex)).Borders.LineStyle = 1                    '设表格边框样式                End With                 With xlSheet.PageSetup                    .LeftHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10公司名称:"   ' & Gsmc                    .CenterHeader = "&""楷体_GB2312,常规""公司人员情况表&""宋体,常规""" & Chr(10) & "&""楷体_GB2312,常规""&10日 期:"                    .RightHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10单位:"                    .LeftFooter = "&""楷体_GB2312,常规""&10制表人:"                    .CenterFooter = "&""楷体_GB2312,常规""&10制表日期:"                    .RightFooter = "&""楷体_GB2312,常规""&10第&P页 共&N页"                End With                 xlApp.Visible = True
      

  12.   

    public void ToExcel(System.Web.UI.Control ctl)  
    {
    // HttpContext.Current.Response.Charset ="GB2312";
    HttpContext.Current.Response.Charset ="";
    HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=money.xls");

    HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("GB2312"); 
    HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
    ctl.Page.EnableViewState =false;
    System.IO.StringWriter  tw = new System.IO.StringWriter() ;
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
    ctl.RenderControl(hw);
    HttpContext.Current.Response.Write(tw.ToString());
    HttpContext.Current.Response.End();
    }然后再调用:ToExcel(datagrid1)
      

  13.   

    public void OutputExcel(DataView dv,string str)
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
                               //dv为要输出到Excel的数据,str为标题名称
    GC.Collect();
    Application excel;// = new Application();
    int rowIndex=4;
    int colIndex=1; _Workbook xBk;
    _Worksheet xSt; excel= new ApplicationClass();

    xBk = excel.Workbooks.Add(true);

    xSt = (_Worksheet)xBk.ActiveSheet; //
    //取得标题
    //
    foreach(DataColumn col in dv.Table.Columns)
    {
    colIndex++;
    excel.Cells[4,colIndex] = col.ColumnName;
    xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
    } //
    //取得表格中的数据
    //
    foreach(DataRowView row in dv)
    {
    rowIndex ++;
    colIndex = 1;
    foreach(DataColumn col in dv.Table.Columns)
    {
    colIndex ++;
    if(col.DataType == System.Type.GetType("System.DateTime"))
    {
    excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
    xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
    }
    else
    if(col.DataType == System.Type.GetType("System.String"))
    {
    excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
    xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
    }
    else
    {
    excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
    }
    }
    }
    //
    //加载一个合计行
    //
    int rowSum = rowIndex + 1;
    int colSum = 2;
    excel.Cells[rowSum,2] = "合计";
    xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
    //
    //设置选中的部分的颜色
    //
    xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();
    xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
    //
    //取得整个报表的标题
    //
    excel.Cells[2,2] = str;
    //
    //设置整个报表的标题格式
    //
    xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
    xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
    //
    //设置报表表格为最适应宽度
    //
    xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
    xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
    //
    //设置整个报表的标题为跨列居中
    //
    xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
    xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
    //
    //绘制边框
    //
    xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
    xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
    xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗
    xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
    xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
    //
    //显示效果
    //
    excel.Visible=true; //xSt.Export(Server.MapPath(".")+"\\"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);
    xBk.SaveCopyAs(Server.MapPath(".")+"\\"+this.xlfile.Text+".xls"); ds = null;
                xBk.Close(false, null,null);

                excel.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
     System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
                xBk = null;
                excel = null;
    xSt = null;
                GC.Collect();
    string path = Server.MapPath(this.xlfile.Text+".xls"); System.IO.FileInfo file = new System.IO.FileInfo(path);
    Response.Clear();
    Response.Charset="GB2312";
    Response.ContentEncoding=System.Text.Encoding.UTF8;
    // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
    // 添加头信息,指定文件大小,让浏览器能够显示下载进度
    Response.AddHeader("Content-Length", file.Length.ToString());

    // 指定返回的是一个不能被客户端读取的流,必须被下载
    Response.ContentType = "application/ms-excel";

    // 把文件流发送到客户端
    Response.WriteFile(file.FullName);
    // 停止页面的执行

    Response.End();
    }