本文介绍了我想在gridview中单独显示Faculty代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在excel记录中如下 星期一会话 日期课程session1 session2 Sub Faculty Sub Faculty 18-Aug BTech1 A RJ B RK BTech2 B SK C SV + MJ 19-Aug BTech4 i MJ j GS BTech5 k RP l CB + RJ 我在Gridview中导出上述excel记录如下。 该代码如下 protected void btnimport_Click( object sender,EventArgs e) { import(); } private void import() { string path = @ C:\ FACULTYT.xlsx GvSch.DataSource = ImportExcelXLS(path,false); GvSch.DataBind(); GvSch .Visible = true; } public static DataSet ImportExcelXLS( string FileName, bool hasHeaders) { string HDR = hasHeaders? 是: 否; string strConn; if (FileName.Substring(FileName.LastIndexOf(' 。'))。ToLower()== 。xlsx) strConn = Provider = Microsoft.ACE.OLEDB.12.0;数据源= + FileName + ;扩展属性= \ Excel 12.0; HDR = + HDR + ; IMEX = 0 \; else strConn = Provider = Microsoft.Jet.OLEDB.4.0; Data Source = + FileName + ;扩展属性= \Excel 8.0; HDR = + HDR + ; IMEX = 0 \; DataSet output = new DataSet(); 使用(OleDbConnection conn = new OleDbConnection(strConn)) { conn.Open(); DataTable schemaTable = conn.GetOleDbSchemaTable( OleDbSchemaGuid.Tables, new object [] { null , null , null , TABLE}); foreach (DataRow schemaRow in schemaTable.Rows) { string sheet = schemaRow [ TABLE_NAME ]的ToString(); if (!sheet.EndsWith( _)) { try { OleDbCommand cmd = new OleDbCommand( SELECT * FROM [ + sheet + ],conn); cmd.CommandType = CommandType.Text; DataTable outputTable = new DataTable(sheet); output.Tables.Add(outputTable); new OleDbDataAdapter(cmd).Fill(outputTable); DataTable table = new DataTable(); table.Columns.Add( Date, typeof ( string )); table.Columns.Add( 课程, typeof ( string )); table.Columns.Add( Session, typeof ( string )); table.Columns.Add( 主题, typeof ( string )); table.Columns.Add( Faculty, typeof ( string )); int irow = 0 ; foreach (DataRow row in outputTable.Rows) { if (row [ 1 ]。ToString()!= 课程&& row [ 1 ]。ToString()!= && row [ 1 ] .ToString()!= null ) { DataRow row1 = table.NewRow(); row1 [ 日期] = row [ 0 ]的ToString(); row1 [ 课程] = row [ 1 ]的ToString(); row1 [ 会话] = 1; row1 [ 主题] = row [ 2 ]的ToString(); row1 [ Faculty] = row [ 3 ]的ToString(); if (row [ 2 ]!= && row [ 3 ]!= ) table.Rows.Add(row1); row1 = table.NewRow(); row1 [ 日期] = row [ 0 ]的ToString(); row1 [ 课程] = row [ 1 ]的ToString(); row1 [ 会话] = 2; row1 [ 主题] = row [ 4 ]的ToString(); row1 [ Faculty] = row [ 5 ]的ToString(); if (row [ 4 ]!= && row [ 5 ]!= ) table.Rows.Add(row1); } irow ++; } output.Tables.Clear(); output.Tables.Add(table); } catch (例外情况) { throw new 异常(ex.Message + string .Format( 工作表:{0} .File:F {1},sheet,FileName),ex); } 最后 { } } } } return 输出; } 当我将上述excel记录输出到Gridview输出时如下 日期课程课程学科教师 18-Aug-14 BTech1 1 A RJ BTech1 2 B RK BTech2 1 B SK BTech2 2 C SV + MJ BTech4 1 I MJ BTech4 2 J GS BTech5 1 K RP BTech5 2 L CB + RJ 但是在Gridview中我希望输出如下,如果教师专栏有多个教师,我想在gridview中单独显示。 gridview中的如果教师专栏有更多的教师,我希望输出如下。 日期课程会话主题t教师 18-Aug-14 BTech1 1 A RJ BTech1 2 B RK BTech2 1 B SK BTech2 2 C SV BTech2 2 C MJ BTech4 1 I MJ BTech4 2 J GS BTech5 1 K RP BTech5 2 L CB BTech5 2 L RJ $ b从我的代码$ b我犯了什么错误请帮助我, 问候, Narasiman P. 解决方案 在将教师分配到第1行[教师]时检查该行[3]& row [5]不等于null并且包含'+'或者不包含'+'然后用'+'符号拆分并向表中添加新行。 代码看起来像 if(row [3] .ToString()。contains(+)) { string [] str = row [3] .ToString()。split('+'); for(int I = 0; i< str.count; i ++)> { row1 [ 课程] = row [ 1 ]。ToString(); row1 [ 会话] = 1; row1 [ 主题] = row [ 2 ]的ToString(); row1 [ Faculty] = str [I]; } } else { < pre lang = cs > row1 [& quot; Course& quot;] = row [1] .ToString(); row1 [& quot; Session& quot;] =& quot; 1& quot ;; row1 [& quot; Subject& quot;] = row [2] .ToString(); row1 [Faculty] = row [3] .ToString(); } In excel record as follows Monday Sessions Date course session1 session2 Sub Faculty Sub Faculty18-Aug BTech1 A RJ B RK BTech2 B SK C SV+MJ19-Aug BTech4 i MJ j GS BTech5 k RP l CB+RJI am exporting the above excel records in Gridview as follows.for that code as followsprotected void btnimport_Click(object sender, EventArgs e){import();}private void import(){string path = @"C:\FACULTYT.xlsxGvSch.DataSource = ImportExcelXLS(path, false);GvSch.DataBind();GvSch.Visible = true;}public static DataSet ImportExcelXLS(string FileName, bool hasHeaders){string HDR = hasHeaders ? "Yes" : "No";string strConn;if (FileName.Substring(FileName.LastIndexOf('.')).ToLower() == ".xlsx")strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";elsestrConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";DataSet output = new DataSet();using (OleDbConnection conn = new OleDbConnection(strConn)){conn.Open();DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });foreach (DataRow schemaRow in schemaTable.Rows){string sheet = schemaRow["TABLE_NAME"].ToString();if (!sheet.EndsWith("_")){try{OleDbCommand cmd = new OleDbCommand("SELECT * FROM [" + sheet + "]", conn);cmd.CommandType = CommandType.Text;DataTable outputTable = new DataTable(sheet);output.Tables.Add(outputTable);new OleDbDataAdapter(cmd).Fill(outputTable);DataTable table = new DataTable();table.Columns.Add("Date", typeof(string));table.Columns.Add("Course", typeof(string));table.Columns.Add("Session", typeof(string));table.Columns.Add("Subject", typeof(string));table.Columns.Add("Faculty", typeof(string));int irow = 0;foreach (DataRow row in outputTable.Rows){if (row[1].ToString() != "Course" && row[1].ToString() != "" && row[1].ToString() != null){DataRow row1 = table.NewRow();row1["Date"] = row[0].ToString();row1["Course"] = row[1].ToString();row1["Session"] = "1";row1["Subject"] = row[2].ToString();row1["Faculty"] = row[3].ToString();if (row[2] != "" && row[3] != "")table.Rows.Add(row1);row1 = table.NewRow();row1["Date"] = row[0].ToString();row1["Course"] = row[1].ToString();row1["Session"] = "2";row1["Subject"] = row[4].ToString();row1["Faculty"] = row[5].ToString();if (row[4] != "" && row[5] != "")table.Rows.Add(row1);}irow++;}output.Tables.Clear();output.Tables.Add(table);}catch (Exception ex){throw new Exception(ex.Message + string.Format("Sheet:{0}.File:F{1}", sheet, FileName), ex);}finally{}}}}return output;}When i export the above excel record in to Gridview output as followsDate Course Session Subject Faculty18-Aug-14 BTech1 1 A RJ BTech1 2 B RK BTech2 1 B SK BTech2 2 C SV+MJ BTech4 1 I MJ BTech4 2 J GS BTech5 1 K RP BTech5 2 L CB+RJBut in Gridview i want the output as follows, if faculty column have more than one faculty i want to display separtely in gridview.in gridview i want output as follows, if faculty column have more that one faculty.Date Course Session Subject Faculty18-Aug-14 BTech1 1 A RJ BTech1 2 B RK BTech2 1 B SK BTech2 2 C SV BTech2 2 C MJ BTech4 1 I MJ BTech4 2 J GS BTech5 1 K RP BTech5 2 L CB BTech5 2 L RJfrom my code what is the mistake i made please help me,Regards,Narasiman P. 解决方案 at the time of assigning Faculty to row1["Faculty"] check that row[3] & row[5] not equal to null and contains '+' or not if contains '+' then split with '+' symbol and add new row to the table. code looks likeif(row[3].ToString().contains("+")){string[] str=row[3].ToString().split('+');for(int I=0;i<str.count;i++)>{row1["Course"] = row[1].ToString();row1["Session"] = "1";row1["Subject"] = row[2].ToString();row1["Faculty"] =str[I]; }}else{<pre lang="cs">row1["Course"] = row[1].ToString();row1["Session"] = "1";row1["Subject"] = row[2].ToString();row1["Faculty"] = row[3].ToString();} 这篇关于我想在gridview中单独显示Faculty代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-18 18:25