My issue is I have searched all over, and one forum told me what I wanted to do couldn't be done without a third party program, while other forums just don't answer my questions. So I am hoping I can get some help here. I had no problem working with and processing the CSV file. There was lots of help for that one. And I was able to find out how to connect to the excel file using OleDbConnection. The coding I have done so far looks like this:
protected void bttnValidate_Click(object sender, EventArgs e)
{
if (docCSV.HasFile)
{
if (CheckFileType(docCSV.FileName))
{
string fileType = Path.GetExtension(docCSV.FileName);
if (fileType == ".csv")
{
Stream theFile = docCSV.PostedFile.InputStream;
using (StreamReader sr = new StreamReader(theFile))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string[] tempArray = line.Split(Convert.ToChar(","));
LoadDataToDatabase(tempArray);
}
InsertData();
DeleteData();
}
Response.Redirect("~/Results.aspx");
}
else if (fileType == ".xls")
{
string strConnXLS;
strConnXLS = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source={0};Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\";", docCSV);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Template$A10:P51]", strConnXLS);
DataSet myDS = new DataSet();
myCommand.Fill(myDS, "ExcelInfo");
}
else if (fileType == ".xlsx")
{
string strConnXLSX;
strConnXLSX = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", docCSV);
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Template$A10:P51]", strConnXLSX);
DataSet myDS = new DataSet();
myCommand.Fill(myDS, "ExcelInfo");
}
else
{
lblInvalidFile.Text = "Invalid file! Please only upload files with extension csv, xls, or xlsx.";
}
}
else
{
lblInvalidFile.Text = "Invalid file! Please only upload files with extension csv, xls, or xlsx.";
}
}
else
{
lblInvalidFile.Text = "Please choose a csv, xls, or xlsx file to upload before clicking on validate. Thank you.";
}
}
The following shows one of the sqlQueries done for the CSV file type.
private void LoadDataToDatabase(string[] tempArray)
{
string sqlTempQuery = string.Empty;
StringBuilder sbTemp = new StringBuilder();
sbTemp.AppendFormat(string.Format(" INSERT INTO TempNumHolder "));
sbTemp.AppendFormat(string.Format(" (SessionID, Temp_FirstNumSet, Temp_SecondNumSet, Temp_ThirdNumSet, Temp_FourthNumSet, Temp_FifthNumSet) "));
sbTemp.AppendFormat(string.Format(" VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}') ", Session.SessionID,
tempArray[0], tempArray[1], tempArray[2], tempArray[3], tempArray[4]));
sqlTempQuery = sbTemp.ToString();
using (SqlConnection sqlConn = new SqlConnection(connectionString))
{
sqlConn.Open();
SqlCommand sqlCmd = new SqlCommand(sqlTempQuery, sqlConn);
sqlCmd.ExecuteNonQuery();
sqlConn.Close();
}
}
So how different is the OleDbCommand to the SqlQuery? Can I just simply modify what I already have to make it work for an excel file, as long as I know where the information I need is? If you would like to see the whole code I will attach the .txt file. Any help would be greatly appreciated. And if there are any sites or questions you would recommend, please include them.
Thanks again!
Attached File(s)
-
excelValidation.txt (7.16K)
Number of downloads: 168

New Topic/Question
Reply




MultiQuote





|