Web.config配置数据库(Excel导入导出举例)
2009-03-28 13:05
621 查看
Code
<connectionStrings>
<!-- SQL connection string for Inventory database lookup -->
<add name="SQLConnetionString" connectionString="server=(local);user id=sa;password=;database=OfficeAutoDB;min pool size=4;max pool size=4;packet size=3072" providerName="System.Data.SqlClient"/>
<add name="ConnectionStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\实例\OfficeAutoDB\App_Data\area.mdb" providerName="System.Data.OleDb" />
</connectionStrings>
<appSettings>
<add key="DSExcel" value="Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="></add>
<add key="Excel" value="App_Data\excel.xls"></add>
<add key="OleConnection" value ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\实例\OfficeAutoDB\App_Data\area.mdb"></add>
<add key="FileService.WebService" value="http://localhost:1052/OfficeAutoDB/WebService.asmx%22/>
</appSettings>
读取配置:
private readonly string DSExcel = ConfigurationSettings.AppSettings["DSExcel"].ToString();
private readonly string Excel = ConfigurationSettings.AppSettings["Excel"].ToString();
public static string sqlcon = ConfigurationManager.ConnectionStrings["SQLConnetionString"].ToString();
-----------------------------------------------------------------------------------------
//导入到Excel
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
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();
}
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "文件上传.xls");
}
//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Control control)
{
}
//读取Excel数据
private DataSet CreateDataSource()
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~\\App_Data\\excel.xls") + ";Extended Properties=Excel 8.0 ;";
OleDbConnection olecon = new OleDbConnection(strCon);
// string ExcelDbPath = DSExcel + Server.MapPath(Excel) + ";";
// OleDbConnection olecon = new OleDbConnection(ExcelDbPath);
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", olecon);
DataSet myds = new DataSet();
myda.Fill(myds);
return myds;
}
//生成Excel表格
/* private void CreateExcelTable()
{
DataSet ds = GetDataSet();
//创建Excel对象
Excel.Application excel = new Excel.Application();
int rowIndex = 1;
int collIndex = 0;
//添加Excel对象的WorkBooks
excel.Application.Workbooks.Add(true);
DataTable table = ds.Tables[0];
//将所得到的表的列名赋给Excel单元格
foreach (DataColumn col in table.Columns)
{//添加列名
collIndex++;
excel.Cells[1, collIndex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{//添加数据
rowIndex++;
collIndex = 0;
foreach (DataColumn col in table.Columns)
{
collIndex++;
excel.Cells[rowIndex, collIndex] = row[col.ColumnName].ToString();
}
}
//不可见即后台处理
excel.Visible = false;
excel.DisplayAlerts = false;
//保存刚才创建的Excel表格
excel.Save(MapPath("App_Data/excel.xls"));
excel.Application.Workbooks.Close();
excel.Application.Quit();
excel.Quit();
//释放使用的Excel对象
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();//调用垃圾收集内存
}
*/
}
<connectionStrings>
<!-- SQL connection string for Inventory database lookup -->
<add name="SQLConnetionString" connectionString="server=(local);user id=sa;password=;database=OfficeAutoDB;min pool size=4;max pool size=4;packet size=3072" providerName="System.Data.SqlClient"/>
<add name="ConnectionStr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\实例\OfficeAutoDB\App_Data\area.mdb" providerName="System.Data.OleDb" />
</connectionStrings>
<appSettings>
<add key="DSExcel" value="Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="></add>
<add key="Excel" value="App_Data\excel.xls"></add>
<add key="OleConnection" value ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\实例\OfficeAutoDB\App_Data\area.mdb"></add>
<add key="FileService.WebService" value="http://localhost:1052/OfficeAutoDB/WebService.asmx%22/>
</appSettings>
读取配置:
private readonly string DSExcel = ConfigurationSettings.AppSettings["DSExcel"].ToString();
private readonly string Excel = ConfigurationSettings.AppSettings["Excel"].ToString();
public static string sqlcon = ConfigurationManager.ConnectionStrings["SQLConnetionString"].ToString();
-----------------------------------------------------------------------------------------
//导入到Excel
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
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();
}
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "文件上传.xls");
}
//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Control control)
{
}
//读取Excel数据
private DataSet CreateDataSource()
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~\\App_Data\\excel.xls") + ";Extended Properties=Excel 8.0 ;";
OleDbConnection olecon = new OleDbConnection(strCon);
// string ExcelDbPath = DSExcel + Server.MapPath(Excel) + ";";
// OleDbConnection olecon = new OleDbConnection(ExcelDbPath);
OleDbDataAdapter myda = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", olecon);
DataSet myds = new DataSet();
myda.Fill(myds);
return myds;
}
//生成Excel表格
/* private void CreateExcelTable()
{
DataSet ds = GetDataSet();
//创建Excel对象
Excel.Application excel = new Excel.Application();
int rowIndex = 1;
int collIndex = 0;
//添加Excel对象的WorkBooks
excel.Application.Workbooks.Add(true);
DataTable table = ds.Tables[0];
//将所得到的表的列名赋给Excel单元格
foreach (DataColumn col in table.Columns)
{//添加列名
collIndex++;
excel.Cells[1, collIndex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{//添加数据
rowIndex++;
collIndex = 0;
foreach (DataColumn col in table.Columns)
{
collIndex++;
excel.Cells[rowIndex, collIndex] = row[col.ColumnName].ToString();
}
}
//不可见即后台处理
excel.Visible = false;
excel.DisplayAlerts = false;
//保存刚才创建的Excel表格
excel.Save(MapPath("App_Data/excel.xls"));
excel.Application.Workbooks.Close();
excel.Application.Quit();
excel.Quit();
//释放使用的Excel对象
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
GC.Collect();//调用垃圾收集内存
}
*/
}
相关文章推荐
- 在WEB页面上将EXCEL文件导入、导出到数据库中
- 使用pentaho工具将数据库数据导入导出为Excel
- C#连接数据库的新方法(通过web.config配置文件)
- C# .Net :Excel NPOI导入导出操作教程之将Excel文件读取并写到数据库表,示例分享
- 加密web.config 导入导出密钥一些资料
- Excel导入导出数据库
- C#数据库数据导入导出系列之一 ASP.NET Excel导入Sql Server数据库 .
- EXCEL导入到数据库之完整版,导出与二进制导入原理相同,这里不做说明!
- .NET中 web.config 配置数据库地址
- ASP.NET中使用web.config配置web应用程序中的数据库连接
- C#数据库数据导入导出系列之四 WinForm数据库导入导出到Excel
- ASP.NET 中Web.config 配置连接数据库
- 023医疗项目-模块二:药品目录的导入导出-从数据库中查出数据用XSSF导出excel并存放在虚拟目录最后下载(包括调试)
- Java实现将Excel导入数据库和从数据库中导出为Excel
- javaweb Excel数据导入数据库
- asp.net2.0WebConfig的配置文件中设置数据库链接
- Java实现Excel导入数据库,数据库中数据导出Excel表
- Excel 导入/导出Oracle 数据库
- web.config配置数据库连接
- C#将数据库导出成Excel,再从Excel导入到数据库中。