C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Application windows, menus & toolbars »

Import Data in Excel to Database


Posted Date: 30 Dec 2008    Resource Type: Code Snippets    Category: Application windows, menus & toolbars
Author: Deepika HaridasMember Level: Diamond    
Rating: 1 out of 5Points: 10



Sample code to Import Data in Excel to Database


Create an Excel app object and open the uploaded file:

Microsoft.Office.Interop.Excel.Application xlsApp = new ApplicationClass();

Workbook wb = xlsApp.Workbooks.Open(_filePath,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing,
Type.Missing,Type.Missing,Type.Missing,Type.Missing);

wb.Unprotect(TemplatePassword);
Save the upload file as a temporary file. Then close the uploaded file and open the temporary file as follows:

string tempFileName = _filePath.ToLower().Replace(".xls","_Temp.xls");
wb.Unprotect(TemplatePassword);
wb.SaveCopyAs(tempFileName);
Verify the data in the template again in the web application:
Collapse
ReadDataSource((Worksheet)wb.Worksheets[DataSourceSheet],
out branchID, out planDate, out startDate, out endDate,
out iChecked, out templatetype);

if(!(iChecked == 1))
{
returnVal = "Please verify the data before upload to the server!";
throw new Exception(returnVal);
}
if(templatetype.ToUpper() != _templatetype.ToUpper())
{
returnVal = "The version is not corrected,
please verify the document and uploaded again";
throw new Exception(returnVal);
}
……
private void ReadDataSource(Worksheet ws, out string branchID,
out string planDate, out DateTime startDate,out DateTime endDate,
out int iChecked, out string templatetype)
{
string check = ws.get_Range("A1", System.Type.Missing).Text.ToString();
branchID = ws.get_Range("A2", System.Type.Missing).Text.ToString();
string sDate = ws.get_Range("A4", System.Type.Missing).Text.ToString();
string eDate = ws.get_Range("A5", System.Type.Missing).Text.ToString();
planDate = ws.get_Range("A6", System.Type.Missing).Text.ToString();
templatetype = ws.get_Range("A7", System.Type.Missing).Text.ToString();
startDate = DateTime.Parse(sDate);
endDate = DateTime.Parse(eDate);
try
{
iChecked = Convert.ToInt16(check);
}
catch
{
iChecked = 0;
}
}



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Import  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Excel insertRows
Previous Resource: Dynamically loading Windows Form
Return to Discussion Resource Index
Post New Resource
Category: Application windows, menus & toolbars


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use