您的位置:首页 > 编程语言 > ASP

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: