導入數據庫
若是表已存在,SQL語句爲:安全
insert into aa select * from OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',服務器
'Data Source=D:"OutData.xls;Extended Properties=Excel 8.0')...[sheet1$]app
其中,aa是表名,D:"OutData.xls是excel的全路徑 sheet1後必須加上$ide
若是表不存在,SQL語句爲:ui
SELECT * INTO aa FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',excel
'Data Source=D:"OutData.xls;Extended Properties=Excel 8.0')...[sheet1$]orm
其中,aa是表名,D:"OutData.xls是excel的全路徑 sheet1後必須加上$對象
可能會發生的異常:接口
若是發生「連接服務器 "(null)" 的 OLE DB 訪問接口 "Microsoft.Jet.OLEDB.4.0" 報錯。提供程序未給出有關錯誤的任何信息。
沒法初始化連接服務器 "(null)" 的 OLE DB 訪問接口 "Microsoft.Jet.OLEDB.4.0" 的數據源對象。」異常多是excel文件未關閉.
若是發生「不能將值 NULL 插入列 'Grade',表 'student.dbo.StuGrade';列不容許有空值。INSERT 失敗。
語句已終止。」異常,則多是excel文件與數據庫表中的字段不匹配
以上操做的是office 2003,若是要操做office 2007則需採用以下方式
若是表已存在,SQL語句爲:
insert into aa select * from OPENDATASOURCE('Microsoft.Ace.OLEDB.12.0',
'Data Source=D:"OutData.xls;Extended Properties=Excel 12.0')...[sheet1$]
其中,aa是表名,D:"OutData.xls是excel的全路徑 sheet1後必須加上$
若是表不存在,SQL語句爲:
SELECT * INTO aa FROM OPENDATASOURCE('Microsoft.Ace.OLEDB.12.0',
'Data Source=D:"OutData.xls;Extended Properties=Excel 12.0')...[sheet1$]
其中,aa是表名,D:"OutData.xls是excel的全路徑 sheet1後必須加上$
若是發生「連接服務器 "(null)" 的 OLE DB 訪問接口 "Microsoft.Jet.OLEDB.4.0" 報錯。提供程序未給出有關錯誤的任何信息。
沒法初始化連接服務器 "(null)" 的 OLE DB 訪問接口 "Microsoft.Jet.OLEDB.4.0" 的數據源對象。」異常多是excel文件未關閉.
若是發生「不能將值 NULL 插入列 'Grade',表 'student.dbo.StuGrade';列不容許有空值。INSERT 失敗。
語句已終止。」異常,則多是excel文件與數據庫表中的字段不匹配
以上操做的是office 2003,若是要操做office 2007則需採用以下方式
另外,還要對一些功能進行配置:
一、打開SQL Server 2005外圍應用配置器,選擇「功能的外圍應用配置器」,選中「啓用OPENROWSET或OPENDATASOURCE支持」,點擊肯定。
二、在C:"WINDOWS目錄下將temp文件夾的安全選項卡中,在用戶或組名稱中,選擇「SQLServer2005ReportingServicesWebServiceUser$PC17$MSSQLSERVER(PC17/SQLServer2005ReportingServicesWebServiceUser$PC17$MSSQLSERVER」用戶,將此用戶的寫入,修改權限選中。點擊肯定。(設置它是由於將此將excel文件讀入SQL數據庫時,是在C:"WINDOWS"temp下創建了一個臨時文件,因此須要將此文件夾的SQLServer2005權限設置爲可寫入的。若是使用的是管理員賬戶,則須要不需此項設置。由於管理員有讀寫的權限。)
導出
使用insert into OPENROWSET('MICROSOFT.JET.OLEDB.4.0','Excel 5.0;HDR=YES;IMEX=YES;DATABASE=C:""Documents and Settings""Administrator""桌面""export2.xls',[sheet1$]) select * from StuGrade能夠將數據導出至excel2003中,但前提必須是表已經存在,字段名都已有且與表對應。而使用下面的自動建立文件和表頭,又會發生異常,插不進去。目前看來只能一條一條插。
解決這個問題能夠先建立一個excel文件並添加表頭,可使用下面的語句:
string filePath = "C:""Documents and Settings""Administrator""桌面""export3.xls";
SqlConnection conn = new SqlConnection("Server=.;Database=Student;Integrated Security=true");
conn.Open();
SqlCommand comm = new SqlCommand("select * from StuGrade", conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds, "StuGrade");
Microsoft.Office.Interop.Excel._Application xlapp = new ApplicationClass();
Workbook xlbook = xlapp.Workbooks.Add(true);
Worksheet xlsheet = (Worksheet)xlbook.Worksheets[1];
int colIndex = 0;
int RowIndex = 1;
//開始寫入每列的標題
foreach (DataColumn dc in ds.Tables[0].Columns)
{
colIndex++;
xlsheet.Cells[RowIndex, colIndex] = dc.Caption;
}
xlbook.Saved = true;
xlbook.SaveCopyAs(filePath); //建立文件
使用這個方法必須添加「using Microsoft.Office.Interop.Excel;」 引用
這樣使用上面那個SQL語句便可實現。
我在與office2007導的時候,將MICROSOFT.JET.OLEDB.4.0和Excel 5.0換成了MICROSOFT.ACE.OLEDB.12.0和Excel 12.0,將表名換成excel2003的表,這樣只能導出一行,並且還會發生異常,這個問題還有待解決。
使用insert into opendatasource('microsoft.jet.oledb.4.0',
'Data source=D:"export.xls;Extended Properties=Excel 5.0')...[Sheet1$]
(字段名) VALUES (對應值)也能夠實現導入。前提必須是表已經存在,字段名都已有且與表對應。並且一次只能導入一條,必須是office2003,換成office2007則能夠導入,但會發生異常。
一般導入與導出用一條SQL語句不太實用,由於用一條SQL語句限制太多,因此大多數狀況下是一條記錄一條記錄寫入數據庫中,使用一條一條導入數據庫的方法以下。其原理是將excel文件看成數據表來用:
導入
代碼爲:
string strExcelFileName = @"D:"OutData.xls"; //excel文件
string strSheetName = "sheet1"; //工做表名
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + strExcelFileName + ";Extended Properties = 'Excel 8.0;HDR=NO;IMEX=1'";
//鏈接字符串
string strExcel = "select * from [" + strSheetName + "$] ";//SQL語句
//定義存放的數據表
DataSet ds = new DataSet();
//鏈接數據源
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
//適配到數據源
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, conn);
adapter.Fill(ds, strSheetName + "$");
conn.Close();
// 通常的狀況下. Excel 表格的第一行是列名
dataGridView1.DataSource = ds.Tables["res"]; //將數據和dataGridView綁定
導出
方法1:從DataSet向excel中導出數據
string filePath = "C:""Documents and Settings""Administrator""桌面""export4.xls"; //導出的文件名和路徑
string ReportName=」aaaaa」; //導出時給文件加上文件頭
SqlConnection conn = new SqlConnection("Server=.;Database=Student;Integrated Security=true"); //定義鏈接
conn.Open();
SqlCommand comm = new SqlCommand("select * from StuGrade", conn);
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds, "StuGrade");
Microsoft.Office.Interop.Excel._Application xlapp = new ApplicationClass();
Workbook xlbook = xlapp.Workbooks.Add(true);
Worksheet xlsheet = (Worksheet)xlbook.Worksheets[1];
Range range = xlsheet.get_Range(xlapp.Cells[1, 1], xlapp.Cells[1, ds.Tables[0].Columns.Count]);
range.MergeCells = true;
//定義單元格中存放文本的樣式
xlapp.ActiveCell.FormulaR1C1 = ReportName;
xlapp.ActiveCell.Font.Size = 20;
xlapp.ActiveCell.Font.Bold = true;
xlapp.ActiveCell.HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlCenter;
int colIndex = 0;
int RowIndex = 2;
//開始寫入每列的標題
foreach (DataColumn dc in ds.Tables[0].Columns)
{
colIndex++;
xlsheet.Cells[RowIndex, colIndex] = dc.Caption;
}
//開始寫入內容
int RowCount = ds.Tables[0].Rows.Count;//行數
for (int i = 0; i < RowCount; i++)
{
RowIndex++;
int ColCount = ds.Tables[0].Columns.Count;//列數
for (colIndex = 1; colIndex <= ColCount; colIndex++)
{
xlsheet.Cells[RowIndex, colIndex] = ds.Tables[0].Rows[i][colIndex - 1];//dg[i, colIndex - 1];
xlsheet.Cells.ColumnWidth = 10;
}
}
xlbook.Saved = true;
xlbook.SaveCopyAs(filePath);
xlapp.Quit();
GC.Collect();
方法2:從DataGridView中向excel導出數據:
SqlConnection conn = new SqlConnection("Server=.;Database=student;Integrated Security=true");
conn.Open();
SqlCommand comm = new SqlCommand("select * from StuGrade where StuID='0000000'", conn); //StuGrade是表名,StuID是字段名
SqlDataAdapter da = new SqlDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds, "StuGrade");
Microsoft.Office.Interop.Excel.Application myExcel = new Microsoft.Office.Interop.Excel.Application();
myExcel.Visible = false;
//定義導出的路徑
string Path = "C:""Documents and Settings""Administrator""桌面";
myExcel.Application.Workbooks.Add(true);
myExcel.Caption = "abcdefghe";
int Colunm = 1;
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
myExcel.Cells[1, Colunm++] = ds.Tables[0].Columns[i].Caption;
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Colunm = 1;
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
myExcel.Cells[i + 2, Colunm++] = ds.Tables[0].Rows[i][j];
}
}
myExcel.ActiveWorkbook.SaveAs(Path, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, null, null, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
myExcel.Quit();
理論上來講,從DataSet中導出和從DataGridView中導出執行的速度應該是同樣的,可是,從我數次的實驗來看,從DataGridView中導出數據比從DataSet中快的多,在數據量爲300條記錄時,用DataGridView比DataSet快2倍左右,這我目前還不知道爲何。這種方法,只適合少許的數據,若是數據量過大,則時間開銷會很大。