我試圖從sql導出數據行到excel,但我的插入命令似乎每次都失敗。我花了很多時間試圖創造這個,但我終於碰壁了。使用OleDb寫入excel
excel文檔是由IRS生成的,我們沒有大聲修改第16行以上的內容。第16行是標題行,下面的所有內容都需要來自sql的數據。標題名稱中都有空格,這似乎是我遇到麻煩的地方。
在行16列名起爲: 與會者名字,與會者姓氏,與會者PTIN,程序號,授予程序CE小時,完成日期
這是我在嘗試寫脫穎而出
private void GenerateReport()
{
FileInfo xlsFileInfo = new FileInfo(Server.MapPath(CE_REPORTS_PATH + CE_PTIN_TEMPLATE + EXTENSION));
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes'", xlsFileInfo.FullName);
//create connection
OleDbConnection oleDBConnection = new OleDbConnection(connectionString);
oleDBConnection.Open();
//create the adapter with the select to get
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$A16:F16]", oleDBConnection);
// Create the dataset and fill it by using the adapter.
DataTable dataTable = new DataTable();
adapter.FillSchema(dataTable, SchemaType.Source);
adapter.Fill(dataTable);
string[] colNames = new string[dataTable.Columns.Count];
string[] colParms = new string[dataTable.Columns.Count];
for (int i = 0; i < dataTable.Columns.Count; i++)
{
colNames[i] = String.Format("[{0}]", dataTable.Columns[i].ColumnName);
colParms[i] = "?";
}
// Create Insert Command
adapter.InsertCommand = new OleDbCommand(String.Format("INSERT INTO [Sheet1$] ({0}) values ({1})", string.Join(",", colNames), string.Join(",", colParms)), oleDBConnection);
// Create Paramaters
for (int i = 0; i < dataTable.Columns.Count; i++)
{
OleDbParameter param = new OleDbParameter(String.Format("@[{0}]", dataTable.Columns[i].ColumnName), OleDbType.Char, 255, dataTable.Columns[i].ColumnName);
adapter.InsertCommand.Parameters.Add(param);
}
// create a new row
DataRow newCERecord = dataTable.NewRow();
// populate row with test data
for (int i = 0; i < dataTable.Columns.Count; i++)
{
newCERecord[i] = "new Data";
}
dataTable.Rows.Add(newCERecord);
// Call update on the adapter to save all the changes to the dataset
adapter.Update(dataTable);
oleDBConnection.Close();
}
我得到的錯誤發生在adapter.Update(dataTable中)被稱爲是如下
$exception {"The INSERT INTO statement contains the following unknown field name: 'Attendee First Name'. Make sure you have typed the name correctly, and try the operation again."} System.Exception {System.Data.OleDb.OleDbException}
釷是令人沮喪的,因爲我直接從colNames [i] = String.Format(「[{0}]」,dataTable.Columns [i] .ColumnName)獲得的列名稱中提取每個字段。我發現我需要[]來解釋列名中的空格,但在這一點上,我不確定問題是什麼。當我看着excel文件時,一切似乎對我都是正確的。
您尚未設置clientsAdapter的UpdateCommand屬性。從我能看到的..我看到它設置爲插入,看看這個StackOverFlow帖子,看看這是否有助於你http://stackoverflow.com/questions/10535663/how-do-i-update-excel-file-with-oledbdataadapter-updatemydataset – MethodMan
我不更新,而是插入到Excel文件中。這個想法是,測試行將出現在Excel文件的下一個。 – Siegeon
你仍然可以按照相同的例子少更新..hope這有助於 – MethodMan