当前位置: 移动技术网 > IT编程>开发语言>.net > EPPlus

EPPlus

2019年02月25日  | 移动技术网IT编程  | 我要评论

昙花图片,吉濑美智子结婚,盖伊女儿枪击遇难

 public class epplus
    {
        public static string excelcontenttype
        {
            get
            {
                return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            }
        }

        #region 示例
        //public filecontentresult export()
        //{
        //    var queryjson = request.params["queryjson"] + "";
        //    datatable dt = new eternalservice().getmenulist(jobject.parse(queryjson));
        //    goto ddd;
        //    ddd:
        //    byte[] filecontent = excelexporthelper.exportexcel(dt, "", false);
        //    return file(filecontent, excelexporthelper.excelcontenttype, "mymenu.xlsx");
        //}
        #endregion
        /// <summary>
        /// 导出excel
        /// </summary>
        /// <param name="datatable">数据源</param>
        /// <param name="heading">工作簿worksheet</param>
        /// <param name="showsrno">//是否显示行编号</param>
        /// <param name="columnstotake">要导出的列</param>
        /// <returns></returns>
        public static byte[] exportexcel(datatable datatable, string heading = "", bool showsrno = false, params string[] columnstotake)
        {
            //建立excel
            byte[] result = null;
            using (excelpackage package = new excelpackage())
            {
                excelworksheet worksheet = package.workbook.worksheets.add(string.format("{0}data", heading));
                //worksheet.workbook.properties.title = "inventory";//设置excel的标题
                worksheet.workbook.properties.author = "凉生";//作者
                //worksheet.workbook.properties.comments = "this is a test";//备注
                worksheet.workbook.properties.company = "xin";//公司

                int startrowfrom = string.isnullorempty(heading) ? 1 : 3;  //开始的行
                //是否显示行编号
                if (showsrno)
                {
                    datacolumn datacolumn = datatable.columns.add("#", typeof(int));
                    datacolumn.setordinal(0);
                    int index = 1;
                    foreach (datarow item in datatable.rows)
                    {
                        item[0] = index;
                        index++;
                    }
                }

                //在excel文件中添加内容
                worksheet.cells["a" + startrowfrom].loadfromdatatable(datatable, true);
                // 小单元格的内容自动调整宽度
                int columnindex = 1;
                foreach (datacolumn item in datatable.columns)
                {
                    excelrange columncells = worksheet.cells[worksheet.dimension.start.row + 1, columnindex, worksheet.dimension.end.row, columnindex];
                    string maxvalue = columncells.max(cell => (cell.value ?? "").tostring());
                    int maxlength = maxvalue.count();
                    if (maxlength < 150) worksheet.column(columnindex).autofit();//自动调整宽度
                    else worksheet.column(columnindex).style.wraptext = true; //自动换行
                    if (regex.ismatch(maxvalue, @"^[+-]?\d*[.]?\d*$"))//验证是否为数字字符串
                    {
                        worksheet.column(columnindex).style.horizontalalignment = excelhorizontalalignment.center;//水平居中
                    }
                    if (item.datatype.equals(type.gettype("system.datetime")))//验证是否是时间
                    {
                        worksheet.column(columnindex).autofit();
                        worksheet.column(columnindex).style.numberformat.format = "yyyy-mm-dd hh:mm:ss";
                    }
                    columnindex++;
                }
                // 格式标题-粗体,黄色的黑色
                using (excelrange r = worksheet.cells[startrowfrom, 1, startrowfrom, datatable.columns.count])
                {
                    r.style.font.color.setcolor(system.drawing.color.white);
                    r.style.font.bold = true;
                    r.style.fill.patterntype = excelfillstyle.solid;
                    r.style.fill.backgroundcolor.setcolor(system.drawing.colortranslator.fromhtml("#1fb5ad"));
                }

                // 格式化单元格-添加边框 
                using (excelrange r = worksheet.cells[startrowfrom + 1, 1, startrowfrom + datatable.rows.count, datatable.columns.count])
                {
                    r.style.border.top.style = excelborderstyle.thin;
                    r.style.border.bottom.style = excelborderstyle.thin;
                    r.style.border.left.style = excelborderstyle.thin;
                    r.style.border.right.style = excelborderstyle.thin;

                    r.style.border.top.color.setcolor(system.drawing.color.black);
                    r.style.border.bottom.color.setcolor(system.drawing.color.black);
                    r.style.border.left.color.setcolor(system.drawing.color.black);
                    r.style.border.right.color.setcolor(system.drawing.color.black);
                }

                // 删除忽略的列
                for (int i = datatable.columns.count - 1; i >= 0; i--)
                {
                    if (i == 0 && showsrno)
                    {
                        continue;
                    }
                    if (columnstotake.contains(datatable.columns[i].columnname))
                    {
                        worksheet.deletecolumn(i + 1);
                    }
                }

                if (!string.isnullorempty(heading))
                {
                    worksheet.cells["a1"].value = heading;
                    worksheet.cells["a1"].style.font.size = 20;

                    worksheet.insertcolumn(1, 1);
                    worksheet.insertrow(1, 1);
                    worksheet.column(1).width = 5;
                }

                result = package.getasbytearray();

            }
            return result;
        }


        #region 示例

        // public actionresult uploadexcle(httppostedfilebase file)
        //{
        //    if (file == null) return content("没有文件!", "text/plain");
        //    var sourcepath = path.combine(server.mappath("~/tempfiles/"), path.getfilename(file.filename));
        //    //建立临时传输文件夹
        //    if (!directory.exists(path.getdirectoryname(sourcepath)))
        //        directory.createdirectory(sourcepath);
        //    file.saveas(sourcepath);
        //    dataset dt = exceltotable.readexceltodataset(sourcepath);
        //    //处理数据
        //    //。。。。。。
        //    //删除文件
        //    system.io.file.delete(sourcepath);
        //    return content("");
        //} 
        #endregion
        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public static dataset readexceltodataset(string filepath)
        {
            dataset ds = new dataset("ds");
            datarow dr;
            object objcellvalue;
            string cellvalue;
            using (filestream fs = new filestream(filepath, filemode.open, fileaccess.readwrite))
            using (excelpackage package = new excelpackage())
            {
                package.load(fs);
                foreach (var sheet in package.workbook.worksheets)
                {
                    if (sheet.dimension == null) continue;
                    var columncount = sheet.dimension.end.column;//获取worksheet的列数
                    var rowcount = sheet.dimension.end.row; //获取worksheet的行数
                    if (rowcount > 0)
                    {
                        datatable dt = new datatable(sheet.name);
                        for (int j = 0; j < columncount; j++)//设置datatable列名  
                        {
                            objcellvalue = sheet.cells[1, j + 1].value;
                            cellvalue = objcellvalue == null ? "" : objcellvalue.tostring();
                            dt.columns.add(cellvalue, typeof(string));
                        }
                        for (int i = 2; i <= rowcount; i++)
                        {
                            dr = dt.newrow();
                            for (int j = 1; j <= columncount; j++)
                            {
                                objcellvalue = sheet.cells[i, j].value;
                                cellvalue = objcellvalue == null ? "" : objcellvalue.tostring();
                                dr[j - 1] = cellvalue;
                            }
                            dt.rows.add(dr);
                        }
                        ds.tables.add(dt);
                    }
                }
            }
            return ds;

        }
    }

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网