2010-03-16 5 views
0

확인. 나는의 GridView에서 XLS에 (그러나 여전히 편집 (읽기 전용되지 수 있도록하는 방법을 모르고 )가)동적 GridView 내용 인쇄

지금은 작업이 없습니다 인쇄 데이터를이 경외감을 쓰레기 코드 내보내기 테이블을 만들었어요 . 과 같은 HTML 형식으로 보낼 수 있지만이 아니며이 아니며 인쇄용 다운로드 파일이 이상합니다.

GridView (행이 ...)에 대해 "인쇄보기"를 만들고 인쇄 할 수 있습니까?

웹 사이트에서 가능합니까? 다운로드하지 않고. 여기 그런데

내가 ... 수출을하는 방법이다

using System; 
using System.Data; 
using System.Configuration; 
using System.IO; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 

using NPOI.HSSF.UserModel; 
using NPOI.HPSF; 
using NPOI.POIFS.FileSystem; 

public class GridViewExportUtil 
{ 
    static HSSFWorkbook hssfworkbook; 

    static MemoryStream WriteToStream() 
    { 
     //Write the stream data of workbook to the root directory 
     MemoryStream file = new MemoryStream(); 
     hssfworkbook.Write(file); 
     return file; 
    } 

    static void InitializeWorkbook() 
    { 
     hssfworkbook = new HSSFWorkbook(); 

     ////create a entry of DocumentSummaryInformation 
     DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); 
     dsi.Company = ""; 
     hssfworkbook.DocumentSummaryInformation = dsi; 

     ////create a entry of SummaryInformation 
     SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); 
     si.Subject = ""; 
     hssfworkbook.SummaryInformation = si; 
    } 
    /// <param name="fileName"></param> 
    /// <param name="gv"></param> 
    public static void Export(string fileName, GridView gv) 
    { 
     HttpContext.Current.Response.Clear(); 
     HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
     HttpContext.Current.Response.Charset = System.Text.Encoding.Unicode.EncodingName; 
     HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode; 
     HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble()); 
     //HttpContext.Current.Response.ContentType = "application/ms-excel"; 
     HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; // NPOI 
     HttpContext.Current.Response.AddHeader(
      "content-disposition", string.Format(
       "attachment; filename=Report.xls"));//, fileName)); // Need .XLS file 
     HttpContext.Current.Response.Clear(); 

     InitializeWorkbook(); 

     HSSFSheet sheet1 = hssfworkbook.CreateSheet("Таблица"); 
     //sheet1.CreateRow(0).CreateCell(0).SetCellValue("Таблица"); 

     using (StringWriter sw = new StringWriter()) 
     { 
       // Create a form to contain the grid 
       Table table = new Table(); 
       // add the header row to the table 
       if (gv.HeaderRow != null) 
       { 
        GridViewExportUtil.PrepareControlForExport(gv.HeaderRow); 
        table.Rows.Add(gv.HeaderRow); 
       } 
       // add each of the data rows to the table 
       foreach (GridViewRow row in gv.Rows) 
       { 
        GridViewExportUtil.PrepareControlForExport(row); 
        table.Rows.Add(row); 
       } 
       // add the footer row to the table 
       if (gv.FooterRow != null) 
       { 
        GridViewExportUtil.PrepareControlForExport(gv.FooterRow); 
        table.Rows.Add(gv.FooterRow); 
       } 

       sheet1.DisplayGridlines = true; 

       HSSFCellStyle style1 = hssfworkbook.CreateCellStyle(); 
       style1.Alignment = HSSFCellStyle.ALIGN_CENTER; 

       sheet1.SetColumnWidth(0, 10000); 
       sheet1.SetColumnWidth(1, 5000); 
       sheet1.VerticallyCenter = true; 

       for (int j = 2; j < table.Rows[0].Cells.Count; j++) 
       { 
        sheet1.SetColumnWidth(j, 4000); 
        sheet1.SetDefaultColumnStyle(short.Parse(j.ToString()), style1); 
       } 

       double Temp=0; 
       for(int i=0; i<(table.Rows.Count-1); i++) 
       { 

        for (int j = 0; j < table.Rows[i].Cells.Count;j++) 
        { 
         if (i != 0 && j != 0) 
         { 
          if (double.TryParse(table.Rows[i].Cells[j].Text, out Temp)) 
          { 
           sheet1.CreateRow(i).CreateCell(j).SetCellValue(Temp.ToString()); 
          } 
         } 
         else 
         { 
          sheet1.CreateRow(i).CreateCell(j).SetCellValue(table.Rows[i].Cells[j].Text); 
         } 
        } 
       } 
       HttpContext.Current.Response.BinaryWrite(WriteToStream().GetBuffer()); 
       HttpContext.Current.Response.End(); 
     } 
    } 

    /// <summary> 
    /// Replace any of the contained controls with literals 
    /// </summary> 
    /// <param name="control"></param> 
    private static void PrepareControlForExport(Control control) 
    { 
     for (int i = 0; i < control.Controls.Count; i++) 
     { 
      Control current = control.Controls[i]; 
      if (current is LinkButton) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text)); 
      } 
      else if (current is ImageButton) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText)); 
      } 
      else if (current is HyperLink) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text)); 
      } 
      else if (current is DropDownList) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text)); 
      } 
      else if (current is CheckBox) 
      { 
       control.Controls.Remove(current); 
       control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False")); 
      } 
      if (current.HasControls()) 
      { 
       GridViewExportUtil.PrepareControlForExport(current); 
      } 
     } 
    } 
} 

답변

1

은 무엇 당신이 할 수있는 것은 인쇄에 자바 스크립트 브라우저의 "인쇄"함수를 호출이다 (어쩌면 내가 어떤 진보^_를 얻을 수 있습니다) 귀하의 페이지. 그리드 뷰뿐만 아니라 페이지의 모든 것을 의미합니다.

+0

큰 실패 나는 수평 스크롤바를 인쇄 할 수 없다 : S – Cynede

+0

가로 스크롤바는 무엇을 의미합니까? – hallie

+0

패널의 가로 그리드 막대에 내 눈금이 있음을 의미합니다./인쇄 할 경우 현재 위치 만 볼 수 있습니다. – Cynede