(一) 導出excel表(將數據庫里的數據表保存為xls的文件)
1.先得出一個表Table。
2.在代碼中建一個excel實例。
在建實例前先引用Microsoft.Office.Interop.Excel組件——添加引用
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Workbooks.Add(true);
3.將table時的數據,循環遍歷插入到excel中。
具體實例如下:
string connectionstr = "Server=192.168.1.10;database=ssh;uid=sa;pwd=1234";
string sqlstr = "select * from medstock";
SqlConnection con = new SqlConnection(connectionstr);
SqlCommand cmd = new SqlCommand(sqlstr,con);
DataSet ds = new DataSet();
DataTable db = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);//由于SqlDataAdapter自身帶有數據庫打開與關閉功能,所以不用手動打開數據庫與關閉。
adp.Fill(ds,"table1");
db = ds.Tables["table1"];
//建列名,根據實際情況而定,即要在excel中顯示的列名;
string[] str=new string[db.Columns.Count];
for (int i = 0; i < str.Length-2; i++)
{
str[i] = db.Columns[i+1].ColumnName;
}
//建excel實例。也就是table的容器;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Workbooks.Add(true);
//先在excel中顯示出table的列名;
for (int i = 0; i < str.Length; i++)
{
excel.Cells[1, i + 1] = str[i];
}
//再向excel中循環添加表中的每一行的每一列;
if (db.Rows.Count > 0)
{
for (int i = 0; i < db.Rows.Count; i++)
{
for (int j = 1; j < db.Columns.Count; j++)
{
string str1 = db.Rows[i][j].ToString();
excel.Cells[i + 2, j] = "'" + str1;
}
}
}
//設置禁止彈出保存和覆蓋的詢問提示框
// excel.DisplayAlerts = false;
// excel.AlertBeforeOverwriting = false;
excel.Save();//保存excel文件
excel.Quit();//確保Excel進程關閉
excel.Visible = true;// 前臺可見 后臺運行
excel = null;
2.在代碼中建一個excel實例。
在建實例前先引用Microsoft.Office.Interop.Excel組件——添加引用
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Workbooks.Add(true);
3.將table時的數據,循環遍歷插入到excel中。
具體實例如下:
string connectionstr = "Server=192.168.1.10;database=ssh;uid=sa;pwd=1234";
string sqlstr = "select * from medstock";
SqlConnection con = new SqlConnection(connectionstr);
SqlCommand cmd = new SqlCommand(sqlstr,con);
DataSet ds = new DataSet();
DataTable db = new DataTable();
SqlDataAdapter adp = new SqlDataAdapter(cmd);//由于SqlDataAdapter自身帶有數據庫打開與關閉功能,所以不用手動打開數據庫與關閉。
adp.Fill(ds,"table1");
db = ds.Tables["table1"];
//建列名,根據實際情況而定,即要在excel中顯示的列名;
string[] str=new string[db.Columns.Count];
for (int i = 0; i < str.Length-2; i++)
{
str[i] = db.Columns[i+1].ColumnName;
}
//建excel實例。也就是table的容器;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Workbooks.Add(true);
//先在excel中顯示出table的列名;
for (int i = 0; i < str.Length; i++)
{
excel.Cells[1, i + 1] = str[i];
}
//再向excel中循環添加表中的每一行的每一列;
if (db.Rows.Count > 0)
{
for (int i = 0; i < db.Rows.Count; i++)
{
for (int j = 1; j < db.Columns.Count; j++)
{
string str1 = db.Rows[i][j].ToString();
excel.Cells[i + 2, j] = "'" + str1;
}
}
}
//設置禁止彈出保存和覆蓋的詢問提示框
// excel.DisplayAlerts = false;
// excel.AlertBeforeOverwriting = false;
excel.Save();//保存excel文件
excel.Quit();//確保Excel進程關閉
excel.Visible = true;// 前臺可見 后臺運行
excel = null;
posted on 2012-05-14 16:39 SkyDream 閱讀(1380) 評論(0) 編輯 收藏 所屬分類: C# WinForm