sql server数据导入到excel
Code
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;
}
}
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;
}
}