天天看点

GridView控件列表数据导出

using

System.Drawing;

System.IO;

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(

        Response.ContentType

application/ms-excel

        Response.Write(

<meta http-equiv=Content-Type content=/"text/html; charset=GB2312/">

this

.EnableViewState

        System.IO.StringWriter oStringWriter

new

System.IO.StringWriter();

        HtmlTextWriter oHtmlTextWriter

HtmlTextWriter(oStringWriter);

        GridView2.RenderControl(oHtmlTextWriter);

        Response.Write(oStringWriter.ToString());

        Response.End();

    }

public

override

VerifyRenderingInServerForm(Control control)

或者

///

定义导出 Excel  Word  的函数

private

Export(

string

FileType,

FileName)

System.Text.Encoding.UTF8;

attachment;filename=

+

HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());

FileType;

        StringWriter tw

StringWriter();

        HtmlTextWriter hw

HtmlTextWriter(tw);

        GridView1.RenderControl(hw);

        Response.Write(tw.ToString());

此方法必重写,否则会出错

Button1_Click(

sender, EventArgs e)  

//

Excel

        Export(

Employee information.xls

Button2_Click(

sender, EventArgs e) 

Word

Export("application/ms-excel", "Employee.doc");

application/ms-word

员工信息.doc

都可以

打开word

open()

fileName

temp.doc

if

(fileName

!=

null

)

        {

filePath

@"

images/

            FileStream MyFileStream

FileStream(filePath

fileName, FileMode.Open);

long

FileSize

MyFileStream.Length;

byte

[] Buffer

[(

int

)FileSize];

            MyFileStream.Read(Buffer,

, (

)FileSize);

            MyFileStream.Close();

            Response.AppendHeader(

HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString());

            Response.ContentType

            Response.BinaryWrite(Buffer);

            Response.End();

        }

else

            Response.Write(

文件不存在!

btnExportWord_Click(

{

    Response.Clear();

    Response.Buffer

true

    Response.AddHeader(

content-disposition

attachment;filename=GridViewExport.doc

    Response.Charset

""

    Response.ContentType

application/vnd.ms-word

    StringWriter sw

    HtmlTextWriter hw

HtmlTextWriter(sw);

    GridView1.AllowPaging

    GridView1.DataBind();

    GridView1.RenderControl(hw);

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

    Response.Flush();

    Response.End();

}

btnExportExcel_Click(

Response.Clear();

Response.Buffer

Response.AddHeader(

attachment;filename=GridViewExport.xls

Response.Charset

Response.ContentType

application/vnd.ms-excel

StringWriter sw

HtmlTextWriter hw

GridView1.AllowPaging

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[

].Style.Add(

green

1

2

3

);  

for

(

i

; 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

(i

%

        row.Cells[

#C2D69B

GridView1.RenderControl(hw);

style to format numbers to string

style

<style> .textmode { } </style>

Response.Write(style);

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

Response.Flush();

Response.End();

iTextSharp.text;

iTextSharp.text.pdf;

iTextSharp.text.html;

iTextSharp.text.html.simpleparser;

btnExportPDF_Click(

application/pdf

attachment;filename=GridViewExport.pdf

    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    StringReader sr

StringReader(sw.ToString());

    Document pdfDoc

Document(PageSize.A4, 10f,10f,10f,0f);

    HTMLWorker htmlparser

HTMLWorker(pdfDoc);

    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

    pdfDoc.Open();

    htmlparser.Parse(sr);

    pdfDoc.Close();

    Response.Write(pdfDoc);

btnExportCSV_Click(

attachment;filename=GridViewExport.csv

application/text

    StringBuilder sb

StringBuilder();

k

; k

GridView1.Columns.Count; k

add separator

        sb.Append(GridView1.Columns[k].HeaderText

'

append new line

    sb.Append(

/r/n

GridView1.Rows.Count; i

            sb.Append(GridView1.Rows[i].Cells[k].Text

        sb.Append(

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

/*

Verifies that the control is rendered

*/

Excel 与 Access 互导入 with ASP.NET

Access

Server.MapPath(

App_Data/contacts.mdb

App_Data/Book1.xls

connect

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=

;Extended Properties=Excel 8.0;

(OleDbConnection conn

OleDbConnection(connect))

(OleDbCommand cmd

OleDbCommand())

  {

    cmd.Connection

conn;

    cmd.CommandText

INSERT INTO [MS Access;Database=

].[Persons] SELECT * FROM [Sheet1$]

    conn.Open();

    cmd.ExecuteNonQuery();

  }

SELECT * INTO [MS Access;Database=

].[New Table] FROM [Sheet1$]

import data

from

excel sheet to griedview directly.

cmd.Connection

cmd.CommandText

SELECT * FROM [Sheet1$]

conn.Open();

OleDbDataReader dr

cmd.ExecuteReader();

GridView1.DataSource

dr;

conn.Close();

Reading Text files

into

Access with ASP.NET

FirstName, SecondName

Joe,Bloggs

Fred,Bassett

Archie,Falls

Doris,Knight

Gladys,Day

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Contacts.mdb

OleDbConnection conn

OleDbConnection(connect);

path

App_Data

query

INSERT INTO Persons (FirstName, SecondName) SELECT FirstName, SecondName FROM

[Text;DATABASE

+ path +

;].[test.txt]

OleDbCommand cmd

OleDbCommand(query, conn);

cmd.ExecuteNonQuery();