c# - How can i split excel sheet values and store it in new excel sheet? -
i have excel sheet having values in 1 column splitted delimiter (|). coumn1|column2|column3|column4 (this column names in 1st row of excelsheet) value1|value2|value33|value4
val1||val3|val4
i want split values separate , create new excelsheet store splitted value.
in new excel sheet want values like.
column1 column2 column3 column4
value1 value2 value3 value4
val val3 val4
how can values this.anyone give me solution this.
i data excel , store values in data table. using 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 * [" + sheetnames[0] + "]" + "where [can status] = 90;"; string status = "select * [" + 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 = 0; // add sheet name string array. foreach (datarow row in dt.rows) { excelsheets[i] = row["table_name"].tostring(); i++; } } return excelsheets; }
you can use below mentioned code data excel columns in datatable
.
system.data.oledb.oledbconnection myconnection ; system.data.dataset dtset ; system.data.oledb.oledbdataadapter mycommand ; myconnection = new system.data.oledb.oledbconnection("provider=microsoft.jet.oledb.4.0;data source='c:\\csharp.net-informations.xls';extended properties=excel 8.0;"); mycommand = new system.data.oledb.oledbdataadapter("select * [sheet1$]", myconnection); mycommand.tablemappings.add("table", "testtable"); dtset = new system.data.dataset(); mycommand.fill(dtset); datagridview1.datasource = dtset.tables[0]; myconnection.close();
note:- need change excel workbook path column names according excel spreadsheet
check below link
Comments
Post a Comment