好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#利用ClosedXML导入EXCEL数据源码


using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Web;

using ClosedXML.Excel;


namespace HDHCMS.Models

{


    public class ExcelData

    {

        public string Name { get; set; }

        public int Age { get; set; }

        // 其他属性...

    }

    public class Import

    {

        public List<ExcelData> ImportExcel(string filePath)

        {

            filePath=HttpContext.Current.Server.MapPath(filePath);

            var dataList = new List<ExcelData>();

            using (var workbook = new XLWorkbook(filePath))

            {

                var worksheet = workbook.Worksheet(1);

                var rows = worksheet.RangeUsed().RowsUsed();


                // 跳过标题行

                foreach (var row in rows.Skip(1))

                {

                    var data = new ExcelData

                    {

                        Name = row.Cell(1).GetValue<string>(),

                        Age = row.Cell(2).GetValue<int>()

                        // 设置其他属性...

                    };

                    dataList.Add(data);

                }

            }

            return dataList;

        }

        public string HdhCmsUpNewFile(string DonforFileMc)

        {

            string DonforFileMl = "/upload/Excel";

            string DonforFileLx = "xlsx,xls";

            float DonforFileDx = 9999999999999;

            string DonforDirMl = "";

            int Qzfs = 0;

            string HdhUpDir = "";

            DonforFileMl = DonforFileMl.Replace("", "");

            string filepath = System.Web.HttpContext.Current.Server.MapPath(DonforDirMl + DonforFileMl); //文件夹名,以日期为文件夹名,图片所在这个地方System.Web.HttpContext.Current.

            string filewjm, filekzm, file_ext, ext, DonforUpFileDir, fpath, sPath2;

            string oldFileName = string.Empty, backStr = string.Empty;

            float filesize;

            DonforUpFileDir = "";

            HttpPostedFile userPostedFile = System.Web.HttpContext.Current.Request.Files[DonforFileMc];

            if (userPostedFile.ContentLength > 20)

            {

                string file_name = System.DateTime.Now.ToString("yyyyMMddHHmmss") + (new Random()).Next(); //新文件名

                string sPath = System.DateTime.Now.ToString("yyyyMMdd"); //创建日期文件夹

                filewjm = Path.GetFileName(userPostedFile.FileName).ToLower();

                filekzm = filewjm.Substring(filewjm.LastIndexOf(".") + 1);

                file_ext = filekzm;

                oldFileName = filewjm.Replace("." + filekzm, "");

                ext = userPostedFile.ContentType.ToLower(); //获取文件类型

                filekzm = filewjm.Substring(filewjm.LastIndexOf(".") + 1);

                filesize = userPostedFile.ContentLength;

                if (DonforFileDx < filesize) { HttpContext.Current.Response.Write("上传文件大小不能超过" + ((DonforFileDx / 1000) / 1000).ToString() + "M"); HttpContext.Current.Response.End(); }

                if (DonforFileLx.IndexOf(filekzm) >= 0)

                {

                    sPath2 = filepath + sPath;

                    if (!Directory.Exists(sPath2)) //判断日期文件夹是否存在

                    {

                        Directory.CreateDirectory(sPath2);//创建日期文件夹

                    }

                    else

                    {

                    }

                    fpath = filepath + sPath + "\\" + file_name + "." + file_ext;

                    while (File.Exists(fpath))

                    {

                        file_name = System.DateTime.Now.ToString("yyyyMMddHHmmss") + (new Random()).Next();

                        fpath = filepath + sPath + "\\" + file_name + "." + file_ext;

                    }

                    DonforUpFileDir = DonforFileMl + sPath + "/" + file_name + "." + file_ext;

                    userPostedFile.SaveAs(fpath);


                }

                else

                {

                    HttpContext.Current.Response.Write("文件格式错误!“" + DonforFileLx + "-" + ext + "-" + filekzm + "”");

                }

                DonforUpFileDir = DonforUpFileDir.Replace(HdhUpDir, "");

                if (Qzfs == 0)

                {

                    backStr = DonforUpFileDir;

                }

                else

                {

                    backStr = oldFileName + "|" + DonforUpFileDir;

                }

            }

            else

            {

                backStr = DonforUpFileDir;

            }

            return backStr;

        }


        public string HdhCmsDelFile(string fileName)

        {

            string strBack = "删除成功!";

            fileName = HttpContext.Current.Server.MapPath(fileName);

            if (File.Exists(fileName))

            {

                try

                {

                    File.Delete(fileName);

                }

                catch (Exception e)

                {

                    strBack = "删除失败,错误信息:<br>" + e.ToString();

                }

            }

            else

            {

                strBack = "删除失败,文件不存在!";

            }

            return strBack;

        }

    }

}


查看更多关于C#利用ClosedXML导入EXCEL数据源码的详细内容...

  阅读:37次