我有一个vb.net函数,它使用oledb创建电子表格,然后将其视为数据库,创建表格和插入值。该函数接收文件名和数据集,如果有效则返回文件名。该功能在我的开发机器上运行得很漂亮,但不是其他PC。以下是我的功能,代码有什么问题吗?有什么建议吗?
编辑:没有抛出错误,生成的文件不包含任何数据。
Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.IO
Imports System.Net.Mail
Imports System.Text
Imports System.Web
Imports Microsoft.Office.Interop
...
Public Shared Function writeToExcelFile(ByVal template As String, ByVal filename As String, ByVal data As DataSet) As String
If File.Exists(filename) Then
File.Delete(filename)
End If
If template <> String.Empty AndAlso filename <> String.Empty Then
File.Copy(template, filename)
End If
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=""Excel 8.0;HDR=Yes;"""
Dim conn As New OleDbConnection(connString)
Try
conn.Open()
Dim cmd As New OleDbCommand()
For Each table As DataTable In data.Tables
Dim tableName As String = table.TableName.Replace(" ", "")
Dim tableCreate As String = "CREATE TABLE [" + tableName + "] ("
Dim sql As String = "INSERT INTO [" + tableName + "$]("
Dim colName As String = String.Empty
For Each col As DataColumn In table.Columns
colName = col.ColumnName.Replace("#", "num")
If colName.Contains(" ") Then
sql += " [" + colName.Replace("'", "") + "],"
Else
sql += " " + colName.Replace("'", "") + ","
End If
tableCreate += " [" + colName + "] varchar(255),"
Next
If tableCreate.EndsWith(",") Then
tableCreate = tableCreate.TrimEnd(New [Char]() {","c})
End If
tableCreate += ") "
cmd = New OleDbCommand(tableCreate, conn)
cmd.ExecuteNonQuery()
If sql.EndsWith(",") Then
sql = sql.TrimEnd(New [Char]() {","c})
End If
sql += ") "
For Each row As DataRow In table.Rows
Dim values As String = " VALUES("
For Each col As DataColumn In table.Columns
Try
values += "'" + cleanString(row(col).ToString()).Substring(0, 250) + "...',"
Catch e As Exception
values += "'" + cleanString(row(col).ToString()) + "',"
End Try
Next
If values.EndsWith(",") Then
values = values.TrimEnd(New [Char]() {","c})
End If
values += ") "
cmd = New OleDbCommand(sql + values, conn)
cmd.ExecuteNonQuery()
Next
Next
conn.Close()
Return filename
Catch e As Exception
Throw New Exception(e.Message)
Return String.Empty
End Try
End Function
答案 0 :(得分:0)
检查系统的区域设置与其他计算机的区域设置。 如果它们不同,请尝试匹配它们。
此外,在使用Office Automation时,您应该强制该线程的文化为en-US。 在调用writeToExcelFile()(C#语法)之前设置它:
System.Threading.Thread.CurrentThread.CultureInfo = new System.Globalization.CultureInfo("en-US");
调用该方法后,恢复培养(如果需要)。
答案 1 :(得分:0)
问题不在于此代码。问题在于传入的数据集。