Data export Excel files in the Asp.net|excel DataTable
<summary>
Export data from a DataTable to a specified Excel file
</summary>
<param name= "page" >web Page object </param>
<param name= "tab" > DataTable object containing exported data </param>
<param name= "FileName" >excel file name </param>
public static void Export (System.Web.UI.Page page,system.data.datatable tab,string FileName)
{
System.Web.HttpResponse HttpResponse = page. Response;
System.Web.UI.WebControls.DataGrid datagrid=new System.Web.UI.WebControls.DataGrid ();
Datagrid.datasource=tab. DefaultView;
Datagrid.allowpaging = false;
DataGrid.HeaderStyle.BackColor = System.Drawing.Color.Green;
DataGrid.HeaderStyle.HorizontalAlign = Horizontalalign.center;
DataGrid.HeaderStyle.Font.Bold = true;
Datagrid.databind ();
Httpresponse.appendheader ("Content-disposition", "attachment;filename=" +httputility.urlencode (filename, SYSTEM.TEXT.ENCODING.UTF8)); Filename= "*.xls";
Httpresponse.contentencoding=system.text.encoding.getencoding ("GB2312");
Httpresponse.contenttype = "Application/ms-excel";
System.IO.StringWriter tw = new System.IO.StringWriter ();
System.Web.UI.HtmlTextWriter HW = new System.Web.UI.HtmlTextWriter (TW);
Datagrid.rendercontrol (HW);
String filePath = page. Server.MapPath ("..") + "\\Files\\" +filename;
System.IO.StreamWriter SW = System.IO.File.CreateText (FilePath);
Sw. Write (TW. ToString ());
Sw. Close ();
Downfile (Httpresponse,filename,filepath);
Httpresponse.end ();
}
private static bool Downfile (System.Web.HttpResponse response,string filename,string fullpath)
{
Try
{
Response.ContentType = "Application/octet-stream";
Response.appendheader ("Content-disposition", "attachment;filename=" +
Httputility.urlencode (Filename,system.text.encoding.utf8) + "; charset=gb2312");
System.IO.FileStream fs= System.IO.File.OpenRead (FullPath);
Long Flen=fs. Length;
int size=102400;//Download Data every 100K
byte[] ReadData = new byte[size];//Specifies the size of the buffer
if (Size>flen) Size=convert.toint32 (Flen);
Long fpos=0;
BOOL Isend=false;
while (!isend)
{
if ((fpos+size) >flen)
{
Size=convert.toint32 (Flen-fpos);
ReadData = new Byte[size];
Isend=true;
}
Fs. Read (readdata, 0, size);/reading a compressed block
Response.BinaryWrite (ReadData);
Fpos+=size;
}
Fs. Close ();
System.IO.File.Delete (FullPath);
return true;
}
Catch
{
return false;
}
}
Converts data in the specified Excel file into a DataTable
<summary>
Converts the data in the specified Excel file into a DataTable object for further processing by the application
</summary>
<param name= "FilePath" ></param>
<returns></returns>
public static System.Data.DataTable Import (String filePath)
{
System.Data.DataTable rs = new System.Data.DataTable ();
BOOL Canopen=false;
OleDbConnection conn = new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data source=" + FilePath + ";" +
"Extended properties=\" Excel 8.0;\ "");
try//Try the data connection is available
{
Conn. Open ();
Conn. Close ();
Canopen=true;
}
catch{}
if (CanOpen)
{
try//try to read data if the data connection can be opened
{
OleDbCommand Myoledbcommand = new OleDbCommand ("SELECT * from [sheet1$]", conn);
OleDbDataAdapter myData = new OleDbDataAdapter (Myoledbcommand);
Mydata.fill (RS);
Conn. Close ();
}
catch//If the data connection can be opened but the read data fails, the name of the worksheet is extracted from the file, and then the data is read
{
String Sheetname=getsheetname (FilePath);
if (sheetname.length>0)
{
OleDbCommand Myoledbcommand = new OleDbCommand ("SELECT * FROM [" +sheetname+ "$]", conn);
OleDbDataAdapter myData = new OleDbDataAdapter (Myoledbcommand);
Mydata.fill (RS);
Conn. Close ();
}
}
}
Else
{
System.IO.StreamReader Tmpstream=file.opentext (FilePath);
String Tmpstr=tmpstream.readtoend ();
Tmpstream.close ();
Rs=getdatatablefromstring (TMPSTR);
Tmpstr=&qu