发布一个Excel导入数据到GridView的类



    public class DataUtil
    {
        
public DataUtil()
        {
        }

        
#region 导入数据到GridView

        
/// <summary>
        
/// 导入数据到GridView
        
/// </summary>
        
/// <param name="fileName">源Excel文件</param>
        
/// <param name="gridview">目标GridView</param>
        
/// <param name="keyColIndex">导入主键列</param>
        
/// <param name="rtnMsg">返回消息</param>
        
/// <returns>返回没有导入的数据DataSet</returns>
        public static DataSet ExcelToGridView(string fileName, GridView gridview, int keyColIndex,string rtnMsg)
        {
            DataSet ds 
= null ;
            
string errMsg = string.Empty;
            
try
            {
                
if ( ! fileName.EndsWith(".xls"))
                {
                    errMsg 
+= "只能导入Excel格式的文件。";
                }

                ds 
= ExcelHelper.ExcelToDS(fileName);
                DataTable dt 
= ds.Tables[0];
                
for (int i = 0; i < gridview.Rows.Count; i++)
                {
                    
if (gridview.Rows[i].RowType != DataControlRowType.DataRow)
                    {
                        
continue;
                    }
                    
string key = gridview.Rows[i].Cells[keyColIndex].Text;
                    
if (String.IsNullOrEmpty(key))
                    {
                        
continue;
                    }
                    
int k = 0;
                    
for (; k < dt.Rows.Count; k++)
                    {
                        
if (dt.Rows[k][gridview.Columns[keyColIndex].HeaderText].ToString().Replace(" """).Replace(" """== key)
                        {
                            
break;
                        }
                    }
                    
if (k == dt.Rows.Count)
                    {
                        
continue;
                    }
                    
else
                    {
                        
for (int j = 0; j < gridview.Columns.Count; j++)
                        {
                            
if (gridview.Columns[j].Visible == false)
                            {
                                
continue;
                            }
                            
if (gridview.Rows[i].Cells[j].Controls.Count == 0)
                            {
                                
continue;
                            }
                            
for (int m = 0; m < gridview.Rows[i].Cells[j].Controls.Count; m++)
                            {
                                
if (gridview.Rows[i].Cells[j].Controls[m].Visible == false)
                                {
                                    
continue;
                                }
                                
if (gridview.Rows[i].Cells[j].Controls[m] is TextBox)
                                {
                                    
if (!dt.Columns.Contains(gridview.Columns[j].HeaderText))
                                    {
                                        
continue;
                                    }
                                    TextBox txt 
= gridview.Rows[i].Cells[j].Controls[m] as TextBox;
                                    
string temp = dt.Rows[k][gridview.Columns[j].HeaderText].ToString();
                                    
try
                                    {
                                        
//           int intTemp = int.Parse(temp);
                                        
//           txt.Text = intTemp.ToString();
                                        txt.Text = temp; //Translate(temp);
                                    }
                                    
catch (Exception)
                                    {
                                    }
                                    
break;
                                }
                            }
                        }
                        dt.Rows[k].Delete();
                        dt.AcceptChanges();
                    }
                }
            }
            
catch (Exception ex)
            {
                errMsg 
+="导入失败."  + ex.Message ;
            }
            rtnMsg 
= errMsg;
            
return ds;
        }

        
/// <summary>
        
/// 导入数据到GridView
        
/// </summary>
        
/// <param name="fileUpload">文件上传控件对象</param>
        
/// <param name="filePath">要存放文件的服务器路径</param>
        
/// <param name="gridview">目标GridView</param>
        
/// <param name="keyColIndex">用于导入的主键列</param>
        
/// <returns>返回没有导入的数据DataSet</returns>
        public static DataSet ExcelToGridView(FileUpload fileUpload, string filePath, GridView gridview, int keyColIndex, string strMsg)
        {
            
string errMsg = string.Empty;
            DataSet ds 
= null ;
            
try
            {
                
if ( ! filePath.EndsWith("\\"))
                {
                    filePath 
+= "\\";
                }
                
string fileName = fileUpload.FileName.Replace(".xls"""+ "_" + DateTime.Now.ToString("yyyyMMddhhmmss"+ ".xls";
                
string fullFileName = filePath + fileName;
                fileUpload.SaveAs(fullFileName);
                ds 
= ExcelToGridView(fullFileName, gridview, keyColIndex,errMsg);
                
if ( errMsg != string.Empty)
                {
                    errMsg 
+= "将数据导入GridView失败." + errMsg ;
                };
            }
            
catch (Exception ex)
            {
                errMsg 
+= "上传excel文件失败." ;
            }
            strMsg 
= errMsg;
            
return ds ;
        }

        
#endregion

        
/// <summary>
        
/// 选定主键列后,用这方法将重复行用黄色背景标志
        
/// </summary>
        
/// <param name="gridview">目标GridView</param>
        
/// <param name="keyColIndex">导入主键列</param>
        public static void MarkRepetitionRows(GridView gridView, int keyColIndex)
        {
            
for (int i = 0; i < gridView.Rows.Count; i++)
            {
                
for (int j = i + 1; j < gridView.Rows.Count; j++)
                {
                    
if (gridView.Rows[i].Cells[keyColIndex].Text == gridView.Rows[j].Cells[keyColIndex].Text)
                    {
                        gridView.Rows[i].BackColor 
= Color.Yellow;
                        gridView.Rows[j].BackColor 
= Color.Yellow;
                    }
                }
            }
        }

    }

ExcelHelper代码

转载请注明出处.
posted @ 2007-05-18 13:18  shiningrise  阅读(4098)  评论(8编辑  收藏  举报
// 侧边栏目录 // https://blog-static.cnblogs.com/files/douzujun/marvin.nav.my1502.css