sql server数据导入到excel
2009-07-21 10:47
453 查看
private void btnDownloadExcel_Click(object sender, EventArgs e)
{
lblMsg.Text = "";
string saveFilePath = txtQueryFilePath.Text;
if (saveFilePath == "")
{
lblMsg.Text = "请输入保存excel的路径!";
return;
}
try
{
progressBar1.Value=1;
string sql = rTxtQuerySql.Text;
if (sql == "")
{
lblMsg.Text = "请输入查询语句!";
return;
}
string strConn = ConfigurationManager.ConnectionStrings["strConn"].ToString();
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
// string sql="select *from GroupCode";
//string sql = "select * From scrap where statusflag='7' and apptime between '2009-06-01' and '2009-06-30'";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "table1");
conn.Close();
System.Data.DataTable dt = new System.Data.DataTable();
dt = ds.Tables[0];
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();//Application与ApplicationClass的区别我没有明白。
Workbook excelBook = excelApp.Workbooks.Add(Type.Missing);
Worksheet excelSheet = (Worksheet)excelBook.ActiveSheet;
int rowCount = dt.Rows.Count;
int colCount = dt.Columns.Count;
progressBar1.Maximum = rowCount*colCount + 10;
progressBar1.Step = 1;
object[,] dataArray = new object[rowCount + 1, colCount];//二维数组定义是多一个标题行。
for (int j = 0; j < colCount; j++)
{
dataArray[0, j] = dt.Columns[j].Caption;//导出字段标题。
for (int i = 0; i < rowCount; i++)
{
dataArray[i + 1, j] = dt.Rows[i][j];
progressBar1.PerformStep();
//progressBar1.Value = progressBar1.Value + 1;
}
}
// excelSheet.get_Range(excelSheet.Cells[startRow, startCol], excelSheet.Cells[rowCount + startRow, colCount + startCol - 1]).Value2 = dataArray;
excelSheet.get_Range("A1", excelSheet.Cells[rowCount + 1, colCount]).Value2 = dataArray;
// excelApp.Visible = true;
excelBook.SaveAs(saveFilePath, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795, null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
excelApp.Quit();
GC.Collect();
lblMsg.Text ="共"+rowCount+ "行数据成功导入到excel,保存在" + saveFilePath;
}
catch (Exception ex)
{
progressBar1.Minimum = 0;
lblMsg.Text = ex.Message;
}
}
{
lblMsg.Text = "";
string saveFilePath = txtQueryFilePath.Text;
if (saveFilePath == "")
{
lblMsg.Text = "请输入保存excel的路径!";
return;
}
try
{
progressBar1.Value=1;
string sql = rTxtQuerySql.Text;
if (sql == "")
{
lblMsg.Text = "请输入查询语句!";
return;
}
string strConn = ConfigurationManager.ConnectionStrings["strConn"].ToString();
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
// string sql="select *from GroupCode";
//string sql = "select * From scrap where statusflag='7' and apptime between '2009-06-01' and '2009-06-30'";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "table1");
conn.Close();
System.Data.DataTable dt = new System.Data.DataTable();
dt = ds.Tables[0];
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();//Application与ApplicationClass的区别我没有明白。
Workbook excelBook = excelApp.Workbooks.Add(Type.Missing);
Worksheet excelSheet = (Worksheet)excelBook.ActiveSheet;
int rowCount = dt.Rows.Count;
int colCount = dt.Columns.Count;
progressBar1.Maximum = rowCount*colCount + 10;
progressBar1.Step = 1;
object[,] dataArray = new object[rowCount + 1, colCount];//二维数组定义是多一个标题行。
for (int j = 0; j < colCount; j++)
{
dataArray[0, j] = dt.Columns[j].Caption;//导出字段标题。
for (int i = 0; i < rowCount; i++)
{
dataArray[i + 1, j] = dt.Rows[i][j];
progressBar1.PerformStep();
//progressBar1.Value = progressBar1.Value + 1;
}
}
// excelSheet.get_Range(excelSheet.Cells[startRow, startCol], excelSheet.Cells[rowCount + startRow, colCount + startCol - 1]).Value2 = dataArray;
excelSheet.get_Range("A1", excelSheet.Cells[rowCount + 1, colCount]).Value2 = dataArray;
// excelApp.Visible = true;
excelBook.SaveAs(saveFilePath, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel9795, null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
excelApp.Quit();
GC.Collect();
lblMsg.Text ="共"+rowCount+ "行数据成功导入到excel,保存在" + saveFilePath;
}
catch (Exception ex)
{
progressBar1.Minimum = 0;
lblMsg.Text = ex.Message;
}
}
相关文章推荐
- 轻松实现SQL Server与Access、Excel数据表间的导入导出
- Sql Server 导入excel数据之bug(转)
- 怎样将Excel中的数据导入到SQL Server 2000数据库中
- SQL SERVER 和EXCEL的数据导入导出
- 将Excel 数据导入到Sql Server 2005
- [导入][转]精妙的SQL和SQL SERVER 与ACCESS、EXCEL的数据导入导出转换
- 轻松实现SQL Server与Access、Excel数据表间的导入导出
- 精妙的SQL和SQL SERVER 与ACCESS、EXCEL的数据导入导出转换
- 怎样将Excel中的数据导入到SQL Server 2000数据库中
- Excel数据导入到Sql Server 2005
- Excel导入SQL Server数据丢失的解决办法
- SQL SERVER 从Excel导入数据时不同语言字符集的比较问题
- SQL SERVER使用OpenRowset,、OpenDataSource函数导入、导出数据到Excel 的几种方法(整理)
- 如何将 Excel 数据导入 SQL Server
- Excel数据导入到Access,Sql Server中
- 64位的Sql Server使用OPENROWSET导入xlsx格式的excel数据的时候报错(转载)
- ASP.NET中如何把EXCEL中数据导入到SQL SERVER
- Excel 数据导入到 Access、Sql Server 中示例代码
- 用sql server导入导出功能 把excel导入到sql的表中,提示导入成功,但是数据库表中并没有数据?
- 轻松实现SQL Server与Access、Excel数据表间的导入导出