概要:
中午睡了一会,醒来的时候看到老师叫我去办公室,需求是这样的,把excel表中的每个同学,判断图片目录中是否有对应的照片(图片的名字用的学号或身份证号码)
没有对应图片的学生记录,存入自己的数据表中或直接输出,最后下载成Excel
于是回去后他把Excel和照片发给我
正文开始:
虽然没接触过Excel的数据导入和将GridView数据导出Excel,在网上查找了很多资料,最后汇总成功实现。
这是第一次写自己的博客并与大家分享。
我也是查了百度学来的,详细地址:
http://jingyan.baidu.com/article/47a29f24003521c0142399dc.html
2.将图片目录所有图片对应的名称导入另外一张表(image表),图片有些多并且如何能达到高效遍历目录文件,于是又去查百度了!地址如下:
http://blog.youkuaiyun.com/love_rrr/article/details/7779403
http://www.cnblogs.com/xdesigner/archive/2006/12/08/586177.html
代码如下:
#region 声明WIN32API函数以及结构 ************************************** [Serializable, System.Runtime.InteropServices.StructLayout (System.Runtime.InteropServices.LayoutKind.Sequential, CharSet = System.Runtime.InteropServices.CharSet.Auto ), System.Runtime.InteropServices.BestFitMapping(false)] private struct WIN32_FIND_DATA { public int dwFileAttributes; public int ftCreationTime_dwLowDateTime; public int ftCreationTime_dwHighDateTime; public int ftLastAccessTime_dwLowDateTime; public int ftLastAccessTime_dwHighDateTime; public int ftLastWriteTime_dwLowDateTime; public int ftLastWriteTime_dwHighDateTime; public int nFileSizeHigh; public int nFileSizeLow; public int dwReserved0; public int dwReserved1; [System.Runtime.InteropServices.MarshalAs (System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 260)] public string cFileName; [System.Runtime.InteropServices.MarshalAs (System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 14)] public string cAlternateFileName; } [System.Runtime.InteropServices.DllImport ("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] private static extern IntPtr FindFirstFile(string pFileName, ref WIN32_FIND_DATA pFindFileData); [System.Runtime.InteropServices.DllImport ("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)] private static extern bool FindNextFile(IntPtr hndFindFile, ref WIN32_FIND_DATA lpFindFileData); [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] private static extern bool FindClose(IntPtr hndFindFile); #endregion //具体方法函数 Stack<string> m_scopes = new Stack<string>(); private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); WIN32_FIND_DATA FindFileData; private System.IntPtr hFind = INVALID_HANDLE_VALUE; void FindFileInDir(string rootDir) { string path = rootDir; start: new FileIOPermission(FileIOPermissionAccess.PathDiscovery, Path.Combine(path, ".")).Demand(); if (path[path.Length - 1] != '\\') { path = path + "\\"; } Response.Write("文件夹为:"+path+"<br>"); hFind = FindFirstFile(Path.Combine(path,"*"), ref FindFileData); if(hFind!=INVALID_HANDLE_VALUE) { do { if (FindFileData.cFileName.Equals(@".") || FindFileData.cFileName.Equals(@"..")) continue; if ((FindFileData.dwFileAttributes & 0x10) != 0) { m_scopes.Push(Path.Combine(path, FindFileData.cFileName)); } else { Response.Write(FindFileData.cFileName+"<br>"); } } while (FindNextFile(hFind, ref FindFileData)); } FindClose(hFind); if (m_scopes.Count >