62,240
社区成员




CREATE TABLE [dbo].[T_Simple](
[Simple] [varchar](500) NULL
)
insert [T_Simple](simple)
values('cdefghijklmnopqrstuvwxyzabcdefghijklcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrsghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz1')
using System;
using System.Data;
using System.Configuration;
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;
/// <summary>
/// WriteExcel 的摘要描述
/// </summary>
public class WriteExcel
{
public static void ToExcel(DataTable dtSource, string strPath, string strSheetName)
{
strPath = @"C:\temp\BooklistInfo.xls";
System.Data.OleDb.OleDbConnection OleDb_Conn = new System.Data.OleDb.OleDbConnection();
OleDb_Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=No';" + "Data Source=\"" + strPath + "\"";
try
{
OleDb_Conn.Open();
System.Data.OleDb.OleDbCommand OleDb_Comm = new System.Data.OleDb.OleDbCommand();
OleDb_Comm.Connection = OleDb_Conn;
string strCmd;
try
{
strCmd = "drop table [" + strSheetName + "]";
OleDb_Comm.CommandText = strCmd;
OleDb_Comm.ExecuteNonQuery();
}
catch
{
}
strCmd = "create Table [" + strSheetName + "](";
foreach (DataColumn dc in dtSource.Columns)
{
strCmd += "[" + dc.ColumnName + "] nvarchar(100),";
}
strCmd = strCmd.Trim().Substring(0, strCmd.Length - 1);
strCmd += ")";
OleDb_Comm.CommandText = strCmd;
OleDb_Comm.ExecuteNonQuery();
foreach (DataRow dr in dtSource.Rows)
{
if (dr.RowState != System.Data.DataRowState.Deleted)
{
strCmd = "insert into [" + strSheetName + "] values(";
foreach (DataColumn dc in dtSource.Columns)
{
strCmd += "'" + dr[dc.ColumnName].ToString().Trim().Replace("'","") + "',";
}
strCmd = strCmd.Substring(0, strCmd.Length - 1);
strCmd += ")";
OleDb_Comm.CommandText = strCmd;
OleDb_Comm.ExecuteNonQuery();
}
}
OleDb_Conn.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
OleDb_Conn.Close();
}
}
public class ExcelIO
{
public static void WriteToExcel(DataTable dtSource, string downloadPath, string modelPath, string downloadFolder)
{
try
{//每次导出之前,先行清理下临时文件夹
string[] files = System.IO.Directory.GetFiles(downloadFolder);
foreach (string file in files)
{
File.Delete(file);
}
System.IO.File.Copy(modelPath, downloadPath, true);
}
catch (Exception ex)
{//如果临时文件夹中的文件有进程在访问,什么也不做
}
System.Data.OleDb.OleDbConnection OleDb_Conn = new OleDbConnection();
string ExcelConn = " Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=No;IMEX=2';Data Source='%ExcelPath%' ";
string strOleConn = ExcelConn;
strOleConn = strOleConn.Replace("%ExcelPath%", downloadPath);
OleDb_Conn.ConnectionString = strOleConn;
try
{
if (OleDb_Conn.State == ConnectionState.Closed)
OleDb_Conn.Open();
OleDbCommand OleDb_Comm = new OleDbCommand();
OleDb_Comm.Connection = OleDb_Conn;
string strSheetName = "Sheet1$";
string strCmd = "";
try
{//为了兼容格式比较长的单元格(比如超过255字段),需要先行删除已有字段
strCmd = "drop table [" + strSheetName + "]";
OleDb_Comm.CommandText = strCmd;
OleDb_Comm.ExecuteNonQuery();
}
catch
{
}
try
{//然后创建新的字段,所有字段都设定为memo类型
strCmd = "create Table [" + strSheetName + "](";
foreach (DataColumn dc in dtSource.Columns)
{
strCmd += "[" + dc.ColumnName + "] memo,";
}
strCmd = strCmd.Trim().Substring(0, strCmd.Length - 1);
strCmd += ")";
OleDb_Comm.CommandText = strCmd;
OleDb_Comm.ExecuteNonQuery();
}
catch
{
}
foreach (DataRow dr in dtSource.Rows)
{
if (dr.RowState != System.Data.DataRowState.Deleted)
{//导出所有数据
strCmd = "insert into [" + strSheetName + "] values(";
foreach (DataColumn dc in dtSource.Columns)
{
strCmd += "'" + dr[dc.ColumnName].ToString().Trim().Replace("'", "\"").Replace("\r\n", " ") + "',";
}
strCmd = strCmd.Substring(0, strCmd.Length - 1);
strCmd += ")";
OleDb_Comm.CommandText = strCmd;
OleDb_Comm.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (OleDb_Conn.State == ConnectionState.Open)
OleDb_Conn.Close();
}
}
public static void DownloadFile(System.Web.UI.Page page, string strFile)
{
page.Response.Clear();
page.Response.ContentType = "application/octet-stream";
page.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(System.IO.Path.GetFileName(strFile).Trim()) + "\"");
page.Response.Flush();
page.Response.WriteFile(strFile);
page.Response.End();
}
}
protected void butToExcel_Click(object sender, EventArgs e)
{
List<IWantToBuy> list = _presenter.GetIWantToBuyList_Buy(ExcelMaxRow);
//准备数据源DataTable
DataTable dt = DB_List.ToDataTable(list);
//为了保证并发导出数据时的冲突,文件名称精确到毫秒
string timestamp = DateTime.Now.ToString("yyyyMMddhhmmssfff");
string downloadPath = System.Web.HttpContext.Current.Server.MapPath("~/Templates/ExportExcel/Download/Admin-Buy-Pending" + timestamp + ".xls");
string modelPath = System.Web.HttpContext.Current.Server.MapPath("~/Templates/ExportExcel/Model/Admin-Buy-Pending.xls");
string downloadFolder = System.Web.HttpContext.Current.Server.MapPath("~/Templates/ExportExcel/Download/");
ExcelIO.WriteToExcel(dt, downloadPath, modelPath, downloadFolder);
ExcelIO.DownloadFile(this.Page, downloadPath);
}
protected void butToExcel_Click(object sender, EventArgs e)
{
List<IWantToBuy> list = _presenter.GetIWantToBuyList_Buy(ExcelMaxRow);
DataTable dt = DB_List.ToDataTable(list);
string downloadPath = System.Web.HttpContext.Current.Server.MapPath("~/Templates/ExportExcel/Download/Admin-Buy-Pending.xls");
string modelPath = System.Web.HttpContext.Current.Server.MapPath("~/Templates/ExportExcel/Model/Admin-Buy-Pending.xls");
ExcelIO.WriteToExcel(dt,downloadPath,modelPath);
ExcelIO.DownloadFile(this.Page, downloadPath);
}
public static void WriteToExcel(DataTable dtSource, string downloadPath, string modelPath)
{
try
{
if (System.IO.File.Exists(downloadPath))
System.IO.File.Delete(downloadPath);
System.IO.File.Copy(modelPath, downloadPath, true);
}
catch (Exception ex)
{
throw ex;
return;
}
System.Data.OleDb.OleDbConnection OleDb_Conn = new OleDbConnection();
string ExcelConn = " Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=No;IMEX=2';Data Source='%ExcelPath%' ";
string strOleConn = ExcelConn;
strOleConn = strOleConn.Replace("%ExcelPath%", downloadPath);
OleDb_Conn.ConnectionString = strOleConn;
try
{
if (OleDb_Conn.State == ConnectionState.Closed)
OleDb_Conn.Open();
OleDbCommand OleDb_Comm = new OleDbCommand();
OleDb_Comm.Connection = OleDb_Conn;
string strSheetName = "Sheet1$";
string strCmd = "";
try
{
strCmd = "drop table [" + strSheetName + "]";
OleDb_Comm.CommandText = strCmd;
OleDb_Comm.ExecuteNonQuery();
}
catch
{
}
strCmd = "create Table [" + strSheetName + "](";
foreach (DataColumn dc in dtSource.Columns)
{
strCmd += "[" + dc.ColumnName + "] memo,";
}
strCmd = strCmd.Trim().Substring(0, strCmd.Length - 1);
strCmd += ")";
OleDb_Comm.CommandText = strCmd;
OleDb_Comm.ExecuteNonQuery();
foreach (DataRow dr in dtSource.Rows)
{
if (dr.RowState != System.Data.DataRowState.Deleted)
{
strCmd = "insert into [" + strSheetName + "] values(";
foreach (DataColumn dc in dtSource.Columns)
{
strCmd += "'" + dr[dc.ColumnName].ToString().Trim().Replace("'", "\"").Replace("\r\n", " ") + "',";
}
strCmd = strCmd.Substring(0, strCmd.Length - 1);
strCmd += ")";
OleDb_Comm.CommandText = strCmd;
OleDb_Comm.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (OleDb_Conn.State == ConnectionState.Open)
OleDb_Conn.Close();
}
}
public static void DownloadFile(System.Web.UI.Page page, string strFile)
{
page.Response.Clear();
page.Response.ContentType = "application/octet-stream";
page.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(System.IO.Path.GetFileName(strFile).Trim()) + "\"");
page.Response.Flush();
page.Response.WriteFile(strFile);
page.Response.End();
}
public static void ToExcel(DataTable dtSource, string strPath, string strSheetName)
{
System.Data.OleDb.OleDbConnection OleDb_Conn = new System.Data.OleDb.OleDbConnection();
OleDb_Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=No';" + "Data Source=\"" + strPath + "\"";
try
{
OleDb_Conn.Open();
System.Data.OleDb.OleDbCommand OleDb_Comm = new System.Data.OleDb.OleDbCommand();
OleDb_Comm.Connection = OleDb_Conn;
string strCmd;
try
{
strCmd = "drop table [" + strSheetName + "]";
OleDb_Comm.CommandText = strCmd;
OleDb_Comm.ExecuteNonQuery();
}
catch
{
}
strCmd = "create Table [" + strSheetName + "](";
foreach (DataColumn dc in dtSource.Columns)
{
strCmd += "[" + dc.ColumnName + "] memo,";
}
strCmd = strCmd.Trim().Substring(0, strCmd.Length - 1);
strCmd += ")";
OleDb_Comm.CommandText = strCmd;
OleDb_Comm.ExecuteNonQuery();
foreach (DataRow dr in dtSource.Rows)
{
if (dr.RowState != System.Data.DataRowState.Deleted)
{
strCmd = "insert into [" + strSheetName + "] values(";
foreach (DataColumn dc in dtSource.Columns)
{
strCmd += "'" + dr[dc.ColumnName].ToString().Trim().Replace("'","") + "',";
}
strCmd = strCmd.Substring(0, strCmd.Length - 1);
strCmd += ")";
OleDb_Comm.CommandText = strCmd;
OleDb_Comm.ExecuteNonQuery();
}
}
OleDb_Conn.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
OleDb_Conn.Close();
}
}