I have a Excel sheet It having all values in one column splitted by delimiter (|). Coumn1|column2|column3|column4 (This column names are in 1st Row of Excelsheet) value1|value2|value33|value4
val1||val3|val4
I want to split the values separate and create new excelsheet store the splitted value.
In new Excel sheet i want to get values like.
Column1 Column2 Column3 Column4
value1 value2 value3 value4
val val3 val4
How can i get the Values like this.anyone give me solution for this.
i will get the data from excel and store the values in data table. For this i was using the following code.
public void LoadExcelData()
{
using (SPSite site = this.WebApplication.Sites[0])
{
using (SPWeb currWeb = site.OpenWeb())
{
string fullpath = "";
SPQuery query = new SPQuery();
SPList currList = currWeb.Lists.TryGetList("JDFieldMapping");
SPListItemCollection itemColl = currList.GetItems(query);
query.ViewFields = string.Concat(
"<FieldRef Name='FilePath' />");
SPListItemCollection oItemCol = currList.GetItems(query);
string directorypath = "";
foreach (SPListItem oItem in oItemCol)
{
directorypath = Convert.ToString(oItem["FilePath"]);
break;
}
var directory = new DirectoryInfo(directorypath);
var FilePath = (from f in directory.GetFiles()
orderby f.LastWriteTime descending
select f).First();
string Filename = FilePath.ToString();
fullpath = directorypath + Filename;
DataTable data = GetFileExtension(fullpath);
}
}
}
public DataTable GetFileExtension(string fullpath)
{
var FileFullpath = fullpath;
string fileExtension = Path.GetExtension(fullpath).ToUpper();
string connectionString = "";
if (fileExtension == ".XLS")
{
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + fullpath + "'; Extended Properties='Excel 8.0;HDR=YES;'";
}
else if (fileExtension == ".XLSX")
{
connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + fullpath + "';Extended Properties='Excel 12.0 Xml;HDR=YES;'";
}
if (!(string.IsNullOrEmpty(connectionString)))
{
string[] sheetNames = GetExcelSheetNames(connectionString);
if ((sheetNames != null) && (sheetNames.Length > 0))
{
OleDbConnection con = new OleDbConnection(connectionString);
// string status = "SELECT * FROM [" + sheetNames[0] + "]" + "WHERE [CAN Status] = 90;";
string status = "SELECT * FROM [" + sheetNames[0] + "]";
OleDbDataAdapter da = new OleDbDataAdapter(status, con);
dt = new DataTable();
da.Fill(dt);
con.Close();
}
}
return dt;
}
private string[] GetExcelSheetNames(string strConnection)
{
var connectionString = strConnection;
String[] excelSheets;
using (var connection = new OleDbConnection(connectionString))
{
connection.Open();
var dt = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
excelSheets = new String[dt.Rows.Count];
int i = 0;
// Add the sheet name to the string array.
foreach (DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
}
return excelSheets;
}