我正在尝试将excel文件上传到Web服务器,然后将其打开以将表读取到列表框,然后选择列表框并重新打开excel文件以将数据绑定到gridview。
//upload file to web server - this works fine
protected void getFile()
{
string importPL = string.Empty;
if (FileUpload1.HasFile)
{
string fileName = Server.HtmlEncode(FileUpload1.FileName);
string extension = System.IO.Path.GetExtension(fileName);
if (StaticHelpers.getApplicationEnvironment() == "D")
{
//dev
importPL = System.Configuration.ConfigurationManager.AppSettings["devPL"];
}
else if (StaticHelpers.getApplicationEnvironment() == "T")
{
//test
importPL = System.Configuration.ConfigurationManager.AppSettings["devPL"];
}
else
{
//prod
importPL = System.Configuration.ConfigurationManager.AppSettings["prodPL"];
}
if ((extension == ".xls" || extension == ".xlsx"))
{
FileInfo fileInfo = new FileInfo(FileUpload1.PostedFile.FileName);
fileName = fileInfo.Name;
string excelFilePath = importPL;
FileUpload1.SaveAs(excelFilePath + fileName);
SpreadSheet = FileUpload1.PostedFile.FileName;
SpreadSheet = excelFilePath + fileName;
lblss.Text = SpreadSheet;
lblSpreadSheet.Text = "SpreadSheet: " + SpreadSheet.ToString();
if (SpreadSheet.Trim() != string.Empty)
{
try
{
ArrayList strTables = GetTableExcel(SpreadSheet);//get list of worksheets then bind to listbox
lstbxWorksheets.DataSource = strTables;
lstbxWorksheets.DataBind();
}
catch (Exception ex)
{
lblMessage.Text = ex.Message.ToString();
}
}
}
}
}
//get list of worksheets I believe this is where I'm having most of my problems
public static ArrayList GetTableExcel(string strFileName)
{
ArrayList strTables = new ArrayList();
Catalog oCatlog = new Catalog();
ADOX.Table oTable = new ADOX.Table();
ADODB.Connection oConn = new ADODB.Connection();
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFileName + ";Extended Properties='Excel 12.0;HDR=YES';";
try
{
oConn.Open(connectionString);
oCatlog.ActiveConnection = oConn;
if (oCatlog.Tables.Count > 0)
{
int item = 0;
foreach (ADOX.Table tab in oCatlog.Tables)
{
if (tab.Type == "TABLE")
{
strTables.Add(tab.Name);
item++;
}
}
}
oConn.Close();
}
catch (Exception ex)
{
//
}
return strTables;
}
//select the worksheet from the list box
protected void lstbxWorksheets_SelectedIndexChanged(object sender, EventArgs e)
{
WorkSheet = lstbxWorksheets.SelectedItem.ToString();
lblWorkSheet.Text = "Worksheet: " + WorkSheet;
SpreadSheet = lblss.Text;
tableName = WorkSheet;
if (tableName != string.Empty)
{
SelectedTable = tableName;
}
else
{
lblMessage.Text = "Select a worksheet.";
}
if ((SelectedTable != string.Empty) && (SelectedTable != null))
{
DataTable dt = GetDataTableExcel(SpreadSheet, SelectedTable);
gvData.DataSource = dt.DefaultView;
gvData.DataBind();
WorkSheet = SelectedTable.ToString();
}
}
//get data from workbook and bind to gridview
public static DataTable GetDataTableExcel(string strFileName, string Table)
{
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strFileName + ";Extended Properties='Excel 12.0;HDR=YES';";
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connectionString);
conn.Open();
string strQuery = "SELECT * FROM [" + Table + "]";
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
System.Data.DataSet ds = new System.Data.DataSet();
adapter.Fill(ds);
conn.Close();
return ds.Tables[0];
}
我甚至将其添加到web.config:
<location path="pl">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
答案 0 :(得分:1)