当前位置: 编程技术>.net/c#/asp.net
ASP.NET中 Execl导出的六种方法实例
来源: 互联网 发布时间:2014-08-25
本文导语: 代码如下: /// /// 导出Excel /// /// /// //方法一: public void ImportExcel(Page page, DataTable dt) { try { ...
代码如下:
///
/// 导出Excel
///
///
///
//方法一:
public void ImportExcel(Page page, DataTable dt)
{
try
{
string filename = Guid.NewGuid().ToString() + ".xls";
string webFilePath = page.Server.MapPath("/" + filename);
CreateExcelFile(webFilePath, dt);
using (FileStream fs = new FileStream(webFilePath, FileMode.OpenOrCreate))
{
//让用户输入下载的本地地址
page.Response.Clear();
page.Response.Buffer = true;
page.Response.Charset = "GB2312";
//page.Response.AppendHeader("Content-Disposition", "attachment;filename=MonitorResult.xls");
page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
page.Response.ContentType = "application/ms-excel";
// 读取excel数据到内存
byte[] buffer = new byte[fs.Length - 1];
fs.Read(buffer, 0, (int)fs.Length - 1);
// 写到aspx页面
page.Response.BinaryWrite(buffer);
page.Response.Flush();
//this.ApplicationInstance.CompleteRequest(); //停止页的执行
fs.Close();
fs.Dispose();
//删除临时文件
File.Delete(webFilePath);
}
}
catch (Exception ex)
{
throw ex;
}
}
方法二:
代码如下:
public void ImportExcel(Page page, DataSet ds)
{
try
{
string filename = Guid.NewGuid().ToString() + ".xls";
string webFilePath = page.Server.MapPath("/" + filename);
CreateExcelFile(webFilePath, ds);
using (FileStream fs = new FileStream(webFilePath, FileMode.OpenOrCreate))
{
//让用户输入下载的本地地址
page.Response.Clear();
page.Response.Buffer = true;
page.Response.Charset = "GB2312";
//page.Response.AppendHeader("Content-Disposition", "attachment;filename=MonitorResult.xls");
page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
page.Response.ContentType = "application/ms-excel";
// 读取excel数据到内存
byte[] buffer = new byte[fs.Length - 1];
fs.Read(buffer, 0, (int)fs.Length - 1);
// 写到aspx页面
page.Response.BinaryWrite(buffer);
page.Response.Flush();
//this.ApplicationInstance.CompleteRequest(); //停止页的执行
fs.Close();
fs.Dispose();
//删除临时文件
File.Delete(webFilePath);
}
}
catch (Exception ex)
{
throw ex;
}
}
方法三:
代码如下:
public void ImportExcel(Page page, DataTable dt1, DataTable dt2, string conditions)
{
try
{
string filename = Guid.NewGuid().ToString() + ".xls";
string webFilePath = page.Server.MapPath("/" + filename);
CreateExcelFile(webFilePath, dt1, dt2, conditions);
using (FileStream fs = new FileStream(webFilePath, FileMode.OpenOrCreate))
{
//让用户输入下载的本地地址
page.Response.Clear();
page.Response.Buffer = true;
page.Response.Charset = "GB2312";
//page.Response.AppendHeader("Content-Disposition", "attachment;filename=MonitorResult.xls");
page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
page.Response.ContentType = "application/ms-excel";
// 读取excel数据到内存
byte[] buffer = new byte[fs.Length - 1];
fs.Read(buffer, 0, (int)fs.Length - 1);
// 写到aspx页面
page.Response.BinaryWrite(buffer);
page.Response.Flush();
//this.ApplicationInstance.CompleteRequest(); //停止页的执行
fs.Close();
fs.Dispose();
//删除临时文件
File.Delete(webFilePath);
}
}
catch (Exception ex)
{
throw ex;
}
}
方法四:
代码如下:
private void CreateExcelFile(string filePath, DataTable dt)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
OleDbConnection oleDbConn = new OleDbConnection();
OleDbCommand oleDbCmd = new OleDbCommand();
try
{
string sSql = "";
oleDbConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + @";Extended ProPerties=""Excel 8.0;HDR=Yes;""";
oleDbConn.Open();
oleDbCmd.CommandType = CommandType.Text;
oleDbCmd.Connection = oleDbConn;
//写列名
sSql = "CREATE TABLE sheet1(";
for (int i = 0; i < dt.Columns.Count; i++)
{
if (i < dt.Columns.Count - 1)
{
if (dt.Columns[i].DataType.Name == "String")
{
sSql += "[" + dt.Columns[i].ColumnName + "] Text,";
}
else if (dt.Columns[i].DataType.Name == "DateTime")
{
sSql += "[" + dt.Columns[i].ColumnName + "] Datetime,";
}
else
{
sSql += "[" + dt.Columns[i].ColumnName + "] Decimal,";
}
}
else
{
if (dt.Columns[i].DataType.Name == "String")
{
sSql += "[" + dt.Columns[i].ColumnName + "] Text)";
}
else if (dt.Columns[i].DataType.Name == "DateTime")
{
sSql += "[" + dt.Columns[i].ColumnName + "] DateTime)";
}
else
{
sSql += "[" + dt.Columns[i].ColumnName + "] Decimal)";
}
}
}
oleDbCmd.CommandText = sSql;
oleDbCmd.ExecuteNonQuery();
for (int j = 0; j < dt.Rows.Count; j++)
{
sSql = "INSERT INTO sheet1 VALUES(";
for (int i = 0; i < dt.Columns.Count; i++)
{
if (i < dt.Columns.Count - 1)
{
if (DBNull.Value.Equals(dt.Rows[j][i]))
{
sSql += "NULL,";
}
else
{
if (dt.Columns[i].DataType.Name == "Decimal")
{
sSql += dt.Rows[j][i].ToString() + ",";
}
else
{
sSql += "'" + dt.Rows[j][i].ToString() + "',";
}
}
}
else
if (DBNull.Value.Equals(dt.Rows[j][i]))
{
sSql += "NULL)";
}
else
{
if (dt.Columns[i].DataType.Name == "Decimal")
{
sSql += dt.Rows[j][i].ToString() + ")";
}
else
{
sSql += "'" + dt.Rows[j][i].ToString() + "')";
}
}
}
oleDbCmd.CommandText = sSql;
oleDbCmd.ExecuteNonQuery();
}
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
//断开连接
oleDbCmd.Dispose();
oleDbConn.Close();
oleDbConn.Dispose();
}
}
方法五:
代码如下:
private void CreateExcelFile(string filePath, DataSet ds)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
OleDbConnection oleDbConn = new OleDbConnection();
OleDbCommand oleDbCmd = new OleDbCommand();
try
{
string sSql = "";
oleDbConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + @";Extended ProPerties=""Excel 8.0;HDR=Yes;""";
oleDbConn.Open();
oleDbCmd.CommandType = CommandType.Text;
oleDbCmd.Connection = oleDbConn;
//写列名
for(int k=0;k