Target Values:
Add a Microsoft.Office.Interop.Excel: reference (.dll) from .NET tab.
using (SPWeb web = new SPSite("SiteUrl/").OpenWeb())
{
string workbookPath = @"C:\Sample.xlsx";
Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();
if (ExcelObj == null)
{
// ("ERROR: EXCEL couldn't be started!");
}
ExcelObj.Visible = true;
Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(workbookPath, 0, true, 5,
"", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);
Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
for (int i = 1; i <= 3; i++)
{
// Defining cells Range. I have 6(a,b,c,d,e,f).
Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A" + i.ToString(), "F" + i.ToString());
System.Array myvalues = (System.Array)range.Cells.Value;
string[] theArray = new string[myvalues.Length];
for (int j = 1; j <= myvalues.Length; j++)
{
if (myvalues.GetValue(1, j) == null)
theArray[j - 1] = "";
else
theArray[j - 1] = (string)myvalues.GetValue(1, j).ToString();
// after adding all cells values into the Array, Adding these into SharePoint Items
if (theArray[myvalues.Length-1] != null) // if (theArray[5] != null) // if u have 6 cells
{
web.AllowUnsafeUpdates = true;
SPList lst = web.Lists.TryGetList("UploadExcelData");
SPListItem addNewItem = lst.Items.Add();
if (theArray[0] != null)
{ // Add Items here into the list
addNewItem["Title"] = (theArray[0]);
}
else { }
if (theArray[1] != null)
{ // Middle Name
addNewItem["MName"] = (theArray[1]);
}
else { addNewItem["MName"] = ""; }
if (theArray[2] != null)
{ //Last Name
addNewItem["LName"] = (theArray[2]);
}
else { addNewItem["LName"] = ""; }
if (theArray[3] != null)
{ //Contact
addNewItem["Contact"] = (theArray[3]);
}
if (theArray[4] != null)
{ // Contact1
addNewItem["Contact1"] = (theArray[4]);
}
if (theArray[5] != null)
{ // Address
addNewItem["Address"] = (theArray[5]);
}
// Updating Lookup Field
if (theArray[6] != null)
{ //CountryLookup
int itemID = 0;
SPList lstExamStatus = web.Lists.TryGetList("Country");
SPListItemCollection lstItemColl = lstExamStatus.Items;
foreach (SPListItem item in lstItemColl)
{
if (item["Country"].ToString() == theArray[6])
{
itemID = item.ID;
break;
}
}
if (itemID != 0)
{
addNewItem["CountryLookup"] = itemID;
}
else { addNewItem["CountryLookup"] = 5; }
}
addNewItem.Update();
lst.Update();
web.AllowUnsafeUpdates = false;
}
}
}
}
}
{
string workbookPath = @"C:\Sample.xlsx";
Microsoft.Office.Interop.Excel.Application ExcelObj = new Microsoft.Office.Interop.Excel.Application();
if (ExcelObj == null)
{
// ("ERROR: EXCEL couldn't be started!");
}
ExcelObj.Visible = true;
Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(workbookPath, 0, true, 5,
"", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);
Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
for (int i = 1; i <= 3; i++)
{
// Defining cells Range. I have 6(a,b,c,d,e,f).
Microsoft.Office.Interop.Excel.Range range = worksheet.get_Range("A" + i.ToString(), "F" + i.ToString());
System.Array myvalues = (System.Array)range.Cells.Value;
string[] theArray = new string[myvalues.Length];
for (int j = 1; j <= myvalues.Length; j++)
{
if (myvalues.GetValue(1, j) == null)
theArray[j - 1] = "";
else
theArray[j - 1] = (string)myvalues.GetValue(1, j).ToString();
// after adding all cells values into the Array, Adding these into SharePoint Items
if (theArray[myvalues.Length-1] != null) // if (theArray[5] != null) // if u have 6 cells
{
web.AllowUnsafeUpdates = true;
SPList lst = web.Lists.TryGetList("UploadExcelData");
SPListItem addNewItem = lst.Items.Add();
if (theArray[0] != null)
{ // Add Items here into the list
addNewItem["Title"] = (theArray[0]);
}
else { }
if (theArray[1] != null)
{ // Middle Name
addNewItem["MName"] = (theArray[1]);
}
else { addNewItem["MName"] = ""; }
if (theArray[2] != null)
{ //Last Name
addNewItem["LName"] = (theArray[2]);
}
else { addNewItem["LName"] = ""; }
if (theArray[3] != null)
{ //Contact
addNewItem["Contact"] = (theArray[3]);
}
if (theArray[4] != null)
{ // Contact1
addNewItem["Contact1"] = (theArray[4]);
}
if (theArray[5] != null)
{ // Address
addNewItem["Address"] = (theArray[5]);
}
// Updating Lookup Field
if (theArray[6] != null)
{ //CountryLookup
int itemID = 0;
SPList lstExamStatus = web.Lists.TryGetList("Country");
SPListItemCollection lstItemColl = lstExamStatus.Items;
foreach (SPListItem item in lstItemColl)
{
if (item["Country"].ToString() == theArray[6])
{
itemID = item.ID;
break;
}
}
if (itemID != 0)
{
addNewItem["CountryLookup"] = itemID;
}
else { addNewItem["CountryLookup"] = 5; }
}
addNewItem.Update();
lst.Update();
web.AllowUnsafeUpdates = false;
}
}
}
}
}
No comments:
Post a Comment