GridView控件列表数据导出

简介: using System.Drawing; using System.IO; using System.

using System.Drawing;
using System.IO;
using System.Text;

protected void Button3_Click( object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer
= false ;
        Response.Charset
= " GB2312 " ;
        Response.AppendHeader(
" Content-Disposition " , " attachment;filename=pkmv_de.xls " );
        Response.ContentEncoding
= System.Text.Encoding.GetEncoding( " GB2312 " );
        Response.ContentType
= " application/ms-excel " ;
        Response.Write(
" <meta http-equiv=Content-Type content=/"text/html; charset=GB2312/"> " );
       
this .EnableViewState = false ;
        System.IO.StringWriter oStringWriter
= new System.IO.StringWriter();
        HtmlTextWriter oHtmlTextWriter
= new HtmlTextWriter(oStringWriter);
        GridView2.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();
    }
public override void VerifyRenderingInServerForm(Control control)
    {
      
    }
  

或者


 
   
/// 定义导出 Excel  Word  的函数
    private void Export( string FileType, string FileName)
    {
        Response.Charset
= " GB2312 " ;
        Response.ContentEncoding
= System.Text.Encoding.UTF8;
        Response.AppendHeader(
" Content-Disposition " , " attachment;filename= " + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
        Response.ContentType
= FileType;
       
this .EnableViewState = false ;
        StringWriter tw
= new StringWriter();
        HtmlTextWriter hw
= new HtmlTextWriter(tw);
        GridView1.RenderControl(hw);
        Response.Write(tw.ToString());
        Response.End();
    }

   
/// 此方法必重写,否则会出错
    public override void VerifyRenderingInServerForm(Control control)
    {
    }

    
protected void Button1_Click( object sender, EventArgs e)   // Excel
    {
        Export(
" application/ms-excel " , " Employee information.xls " );
    }

   
protected void Button2_Click( object sender, EventArgs e)  // Word
    {
       
// Export("application/ms-excel", "Employee.doc");
        Export( " application/ms-word " , " 员工信息.doc " ); // 都可以
    }












打开word

using System.IO;
using System.Text;

protected void open()
    {
       
string fileName = " temp.doc " ;
       
if (fileName != null )
        {
           
string filePath = @" images/ " ;
            FileStream MyFileStream
= new FileStream(filePath + fileName, FileMode.Open);
           
long FileSize = MyFileStream.Length;
           
byte [] Buffer = new byte [( int )FileSize];
            MyFileStream.Read(Buffer,
0 , ( int )FileSize);
            MyFileStream.Close();
            Response.AppendHeader(
" Content-Disposition " , " attachment;filename= " + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString());
            Response.ContentType
= " application/ms-word " ;
            Response.BinaryWrite(Buffer);
            Response.End();
        }
       
else
        {
            Response.Write(
" 文件不存在! " );
        }
    }







protected void btnExportWord_Click( object sender, EventArgs e)

{

    Response.Clear();

    Response.Buffer
= true ;

    Response.AddHeader(
" content-disposition " ,

   
" attachment;filename=GridViewExport.doc " );

    Response.Charset
= "" ;

    Response.ContentType
= " application/vnd.ms-word " ;

    StringWriter sw
= new StringWriter();

    HtmlTextWriter hw
= new HtmlTextWriter(sw);

    GridView1.AllowPaging
= false ;

    GridView1.DataBind();

    GridView1.RenderControl(hw);

    Response.Output.Write(sw.ToString());

    Response.Flush();

    Response.End();

}







protected void btnExportExcel_Click( object sender, EventArgs e)

{

Response.Clear();

Response.Buffer
= true ;

 

Response.AddHeader(
" content-disposition " ,

" attachment;filename=GridViewExport.xls " );

Response.Charset
= "" ;

Response.ContentType
= " application/vnd.ms-excel " ;

StringWriter sw
= new StringWriter();

HtmlTextWriter hw
= new HtmlTextWriter(sw);

 

GridView1.AllowPaging
= false ;

GridView1.DataBind();

 

// Change the Header Row back to white color

GridView1.HeaderRow.Style.Add(
" background-color " , " #FFFFFF " );

 

// Apply style to Individual Cells

GridView1.HeaderRow.Cells[
0 ].Style.Add( " background-color " , " green " );

GridView1.HeaderRow.Cells[
1 ].Style.Add( " background-color " , " green " );

GridView1.HeaderRow.Cells[
2 ].Style.Add( " background-color " , " green " );

GridView1.HeaderRow.Cells[
3 ].Style.Add( " background-color " , " green " );  

 

for ( int i = 0 ; i < GridView1.Rows.Count;i ++ )

{

    GridViewRow row
= GridView1.Rows[i];

 

   
// Change Color back to white

    row.BackColor
= System.Drawing.Color.White;

 

   
// Apply text style to each Row

    row.Attributes.Add(
" class " , " textmode " );

 

   
// Apply style to Individual Cells of Alternating Row

   
if (i % 2 != 0 )

    {

        row.Cells[
0 ].Style.Add( " background-color " , " #C2D69B " );

        row.Cells[
1 ].Style.Add( " background-color " , " #C2D69B " );

        row.Cells[
2 ].Style.Add( " background-color " , " #C2D69B " );

        row.Cells[
3 ].Style.Add( " background-color " , " #C2D69B " );  

    }

}

GridView1.RenderControl(hw);

 

// style to format numbers to string

string style = @" <style> .textmode { } </style> " ;

Response.Write(style);

Response.Output.Write(sw.ToString());

Response.Flush();

Response.End();

}





using iTextSharp.text;

using iTextSharp.text.pdf;

using iTextSharp.text.html;

using iTextSharp.text.html.simpleparser;





protected void btnExportPDF_Click( object sender, EventArgs e)

{

    Response.ContentType
= " application/pdf " ;

    Response.AddHeader(
" content-disposition " ,

    
" attachment;filename=GridViewExport.pdf " );

    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    StringWriter sw
= new StringWriter();

    HtmlTextWriter hw
= new HtmlTextWriter(sw);

    GridView1.AllowPaging
= false ;

    GridView1.DataBind();

    GridView1.RenderControl(hw);

    StringReader sr
= new StringReader(sw.ToString());

    Document pdfDoc
= new Document(PageSize.A4, 10f,10f,10f,0f);

    HTMLWorker htmlparser
= new HTMLWorker(pdfDoc);

    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

    pdfDoc.Open();

    htmlparser.Parse(sr);

    pdfDoc.Close();

    Response.Write(pdfDoc);

    Response.End();

}



protected void btnExportCSV_Click( object sender, EventArgs e)

{

    Response.Clear();

    Response.Buffer
= true ;

    Response.AddHeader(
" content-disposition " ,

    
" attachment;filename=GridViewExport.csv " );

    Response.Charset
= "" ;

    Response.ContentType
= " application/text " ;

 

    GridView1.AllowPaging
= false ;

    GridView1.DataBind();

 

    StringBuilder sb
= new StringBuilder();

   
for ( int k = 0 ; k < GridView1.Columns.Count; k ++ )

    {

       
// add separator

        sb.Append(GridView1.Columns[k].HeaderText
+ ' , ' );

    }

   
// append new line

    sb.Append(
" /r/n " );

   
for ( int i = 0 ; i < GridView1.Rows.Count; i ++ )

    {

       
for ( int k = 0 ; k < GridView1.Columns.Count; k ++ )

        {

           
// add separator

            sb.Append(GridView1.Rows[i].Cells[k].Text
+ ' , ' );

        }

       
// append new line

        sb.Append(
" /r/n " );

    }

    Response.Output.Write(sb.ToString());

    Response.Flush();

    Response.End();

}

public override void VerifyRenderingInServerForm(Control control)

{

   
/* Verifies that the control is rendered */

}




Excel 与 Access 互导入 with ASP.NET

string Access = Server.MapPath( " App_Data/contacts.mdb " );
string Excel = Server.MapPath( " App_Data/Book1.xls " );
string connect = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Excel + " ;Extended Properties=Excel 8.0; " ;
using (OleDbConnection conn = new OleDbConnection(connect))
{
 
using (OleDbCommand cmd = new OleDbCommand())
  {
    cmd.Connection
= conn;
    cmd.CommandText
= " INSERT INTO [MS Access;Database= " + Access + " ].[Persons] SELECT * FROM [Sheet1$] " ;
    conn.Open();
    cmd.ExecuteNonQuery();
  }
}




string Access = Server.MapPath( " App_Data/contacts.mdb " );
string Excel = Server.MapPath( " App_Data/Book1.xls " );
string connect = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Excel + " ;Extended Properties=Excel 8.0; " ;
using (OleDbConnection conn = new OleDbConnection(connect))
{
 
using (OleDbCommand cmd = new OleDbCommand())
  {
    cmd.Connection
= conn;
    cmd.CommandText
= " SELECT * INTO [MS Access;Database= " + Access + " ].[New Table] FROM [Sheet1$] " ;
    conn.Open();
    cmd.ExecuteNonQuery();
  }
}




import data
from excel sheet to griedview directly.

string Access = Server.MapPath( " App_Data/contacts.mdb " );
string Excel = Server.MapPath( " App_Data/Book1.xls " );
string connect = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Excel + " ;Extended Properties=Excel 8.0; " ;
using (OleDbConnection conn = new OleDbConnection(connect))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection
= conn;
cmd.CommandText
= " SELECT * FROM [Sheet1$] " ;
conn.Open();
OleDbDataReader dr
= cmd.ExecuteReader();
GridView1.DataSource
= dr;
GridView1.DataBind();
conn.Close();

}
}



Reading Text files
into Access with ASP.NET


/*
FirstName, SecondName
Joe,Bloggs
Fred,Bassett
Archie,Falls
Doris,Knight
Gladys,Day
*/

string connect = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Contacts.mdb " ;
OleDbConnection conn
= new OleDbConnection(connect);
string path = Server.MapPath( " App_Data " );
string query = " INSERT INTO Persons (FirstName, SecondName) SELECT FirstName, SecondName FROM
[Text;DATABASE = " + path + " ;].[test.txt] " ;
OleDbCommand cmd = new OleDbCommand(query, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();

相关文章
|
7月前
|
开发框架 前端开发 JavaScript
在DevExpress的GridView的列中,动态创建列的时候,绑定不同的编辑处理控件
在DevExpress的GridView的列中,动态创建列的时候,绑定不同的编辑处理控件
|
7月前
|
开发框架 前端开发 JavaScript
在DevExpress的GridView的列中,使用RepositoryItemSearchLookUpEdit控件实现产品列表信息的展示和选择
在DevExpress的GridView的列中,使用RepositoryItemSearchLookUpEdit控件实现产品列表信息的展示和选择
|
10月前
使用ListView控件展示数据
使用ListView控件展示数据
Qt实现单击或双击QTableWidge/View表头进行排序
Qt实现单击或双击QTableWidge/View表头进行排序
989 0
|
前端开发 JavaScript
原生全选多选table表格实现
原生全选多选table表格实现
554 0
swing表格JTalble添加数据(数据类型实现自定类型,如颜色,组合框等控件)还有实现控件的功能—添加事件
swing表格JTalble添加数据(数据类型实现自定类型,如颜色,组合框等控件)还有实现控件的功能—添加事件
269 0
swing表格JTalble添加数据(数据类型实现自定类型,如颜色,组合框等控件)还有实现控件的功能—添加事件
|
C#
C# DataGridview控件自动下拉到最后一行
有时候使用DataGridView难免会在最后插入一条数据,如果插入的数据超过滚动条显示的行数,那么默认情况下不会显示到最后一行。增加以下代码一直将滚动条拉倒最低。 this.dataGridView1.
2829 0
|
.NET 开发框架 Go
GridView控件自定义分页的实现
前人栽树,后人乘凉,话不多说,代码如下:     实现方式一: .aspx: [c-sharp] view plain copy <form id="form1" runat="server">       <table style="width: 605px">         .
1443 0