.xls format Office2003 and below
.xlsx format Office2007 and above
.csv format Comma-separated string text (the above two file types can be saved as this format)
There are two different methods to read the first two formats and read the latter format.
Let's see the program below:
Page front desk:
<div> <%-- File Upload Control Used to upload the file to be read and obtain the file information through this control --%>
<asp:FileUpload ID="fileSelect" runat="server" />
<%-- Click this button to execute the reading method--%>
<asp:Button ID="btnRead" runat="server" Text="ReadStart" />
</div>
Background code:
//Declare variables (attributes)
string currFilePath = ; //The full path of the file to be read
string currFileExtension = ; //File extension
//Page_Load event Register button click event
protected void Page_Load(object sender,EventArgs e)
{
+= new EventHandler(btnRead_Click);
}
//Button click event //The 3 methods inside will be given below
protected void btnRead_Click(object sender,EventArgs e)
{
Upload(); //Upload file method
if( ==".xlsx" || ==".xls")
{
DataTable dt = ReadExcelToTable(currFilePath); //Read Excel files (.xls and .xlsx formats)
}
else if( == ".csv")
{
DataTable dt = ReadExcelWidthStream(currFilePath); //Read .csv format file
}
}
The following lists 3 methods of button click event
///<summary>
///Upload the file to the temporary directory
///</ummary>
private void Upload()
{
HttpPostedFile file = ;
string fileName = ;
string tempPath = (); //Get the system temporary file path
fileName = (fileName); //Get file name (without path)
= (fileName); //Get the file extension
= tempPath + fileName; //Get the uploaded file path and record to the global variable declared earlier
(); //Upload
}
///<summary>
///How to read an Excel file in xls\xlsx format
///</ummary>
///<param name="path">The full path to Excel to be read</param>
///<returns></returns>
private DataTable ReadExcelToTable(string path)
{
//Connection string
string connstring = "Provider=.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07 and above cannot have extra spaces, and pay attention to semicolons
//string connstring = Provider=.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; //Office 07 version below because I use Office2010, this connection string is not used. You can choose according to your own situation or the program determines which connection string to use
using(OleDbConnection conn = new OleDbConnection(connstring))
{
();
DataTable sheetsName = (,new object[]{null,null,null,"Table"}); //Get the names of all sheets
string firstSheetName = [0][2].ToString(); //Get the name of the first sheet
string sql = ("SELECT * FROM [{0}],firstSheetName); //Query string
OleDbDataAdapter ada =new OleDbDataAdapter(sql,connstring);
DataSet set = new DataSet();
(set);
return [0];
}
}
///<summary>
/// Method to read Excel file in csv format
///</ummary>
///<param name="path">The full path to Excel to be read</param>
///<returns></returns>
private DataTable ReadExcelWithStream(string path)
{
DataTable dt = new DataTable();
bool isDtHasColumn = false; //Tag whether the column has been generated by DataTable
StreamReader reader = new StreamReader(path,); //Data stream
while(!)
{
string meaage = ();
string[] splitResult = (new char[]{','},); //Read a line, separated by commas, and save into array
DataRow row = ();
for(int i = 0;i<;i++)
{
if(!isDtHasColumn) //If no column has been generated
{
("column" + i,typeof(string));
}
row[i] = splitResult[i];
}
(row); //Add row
isDtHasColumn = true; //After reading the first row, marking that the existing column is already there. When reading the subsequent rows, no column will be generated.
}
return dt;
}