ASP.NET Excel导入导出SQL Server整理(转)
2015-04-03 16:55
495 查看
一、Excel导入
在页面层添加FileUpload控件,控件位置:Standard里面,Calendar下面两个。设置其ID。
<asp:FileUpload ID="inputFile" runat="server" />
添加Button,btnUpload
SqlConnection _con = new SqlConnection(@"Data Source=STKWX028\SQLEXPRESS;Initial Catalog=Library;Integrated Security=True");
string queryStr = "SELECT * FROM bookInfo";
DataSet ds = new DataSet();
SqlDataAdapter ad = new SqlDataAdapter(queryStr, _con);
ad.Fill(ds);
Microsoft.Office.Interop.Excel.Application excelApp = new Application();
if (excelApp == null)
{
lblMessage.Text = "Fail to export because there's no excel installation!";
}
Workbook workbook = excelApp.Workbooks.Add(Missing.Value);
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];//get the sheet index
for (int row = 0; row < ds.Tables[0].Rows.Count;row++ )//write data to excel
{
for (int col = 0; col < ds.Tables[0].Columns.Count;col++ )
{
worksheet.Cells[row + 2, col + 1] = ds.Tables[0].Rows[row][col];
}
}
excelApp.Quit();
转自http://www.cnblogs.com/eva_2010/articles/1884365.html
在页面层添加FileUpload控件,控件位置:Standard里面,Calendar下面两个。设置其ID。
<asp:FileUpload ID="inputFile" runat="server" />
添加Button,btnUpload
SqlConnection _con = new SqlConnection(@"Data Source=STKWX028\SQLEXPRESS;Initial Catalog=Library;Integrated Security=True");
string queryStr = "SELECT * FROM bookInfo";
DataSet ds = new DataSet();
SqlDataAdapter ad = new SqlDataAdapter(queryStr, _con);
ad.Fill(ds);
Microsoft.Office.Interop.Excel.Application excelApp = new Application();
if (excelApp == null)
{
lblMessage.Text = "Fail to export because there's no excel installation!";
}
Workbook workbook = excelApp.Workbooks.Add(Missing.Value);
Worksheet worksheet = (Worksheet)workbook.Worksheets[1];//get the sheet index
for (int row = 0; row < ds.Tables[0].Rows.Count;row++ )//write data to excel
{
for (int col = 0; col < ds.Tables[0].Columns.Count;col++ )
{
worksheet.Cells[row + 2, col + 1] = ds.Tables[0].Rows[row][col];
}
}
excelApp.Quit();
转自http://www.cnblogs.com/eva_2010/articles/1884365.html
相关文章推荐
- sql server 与 excel 互导以及在asp.net中从DataTable导出到excel
- ASP.NET Excel数据导入SQL Server
- Asp.Net下导出/导入规则的Excel(.xls)文档
- Asp.Net下导出/导入规则的Excel(.xls)文件
- ASP.NET中如何把EXCEL中数据导入到SQL SERVER
- Asp.Net页面导入和导出到EXCEL
- sql server 与 excel 互导以及在asp.net中从DataTable导出到excel
- ASP.NET对EXCEL的导入导出
- asp.net 导入和导出Excel
- Asp.Net下导出/导入规则的Excel(.xls)文件
- Asp.net中关于excel文件批量导入SQL Server数据表
- [导入]Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
- asp.net导入导出EXCEL
- asp.net导出/导入规则的Excel(.xls)文件
- SQL Server中导入/导出 Excel (包括SQL实现和Net代码实现)的基本方法
- SQL SERVER使用OpenRowset,、OpenDataSource函数导入、导出数据到Excel 的几种方法(整理)
- [导入]asp.net 把datatable的数据导出到EXCEL
- sql server 与 excel 互导以及在asp.net中从DataTable导出到excel
- asp.net中Excel导入,导出
- sql server 与 excel 互导以及在asp.net中从DataTable导出到excel