我正在尝试做这样的事情:
public void ImportClick(object sender, EventArgs e) //the button used after selecting the spreadsheet file
{
if (fileUpload.HasFile) //ASP.Net FileUpload control
{
if (fileUpload.FileName.EndsWith(".xls", StringComparison.OrdinalIgnoreCase) || fileUpload.FileName.EndsWith(".xlsx", StringComparison.OrdinalIgnoreCase))
{
Excel sheet = new Excel(fileUpload.Open()); //not sure how to do this part
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["our_database"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("INSERT INTO table_name SELECT * FROM " + sheet, connection))
{
connection.Open();
command.ExecuteQuery(); //Intellisense only has "ExecuteNonQuery()" method, but that's for T-SQL
connection.Close();
}
}
}
else
{
error.Text = "File must be either *.xls or *.xlsx";
error.Visible = true;
}
}
else
{
error.Text = "No file was selected";
error.Visible = true;
}
}
Microsoft.Office.Interop.Excel 命名空间中有很多类和接口,我不知道该用哪一个。
我知道制作 Excel 对象以及 SQL 命令可能不会像我在这里那样容易,但这是我需要帮助的两件事。
任何建议/建议将不胜感激!