protected void Page_Load(object sender, EventArgs e)
    {
        m_SystemFactory = new SystemFactory();
        m_AuthFactory = new AuthFactory();
        m_LogService = m_SystemFactory.GetLogService();
        m_AuthSevice = m_AuthFactory.GetAuthService();

        if (!Page.IsPostBack)
        {
            InitData();
            fillGridView();
        }
    }
    public WebMailService()
    {
        m_ConfigHelper = new ConfigHelper();
        m_SystemFactory = new SystemFactory();
        m_SystemService = m_SystemFactory.GetSystemService();

        m_MailVO = m_SystemService.GetSystemParamByRoot();

        bool enableSSL = m_MailVO.EnableSSL;
        int port = 25;

        if (m_MailVO.MailSmtp.IndexOf("gmail") != -1)
        {
            enableSSL = true;
            port = 587;
        }
        else if (!string.IsNullOrEmpty(m_MailVO.MailPort))
        {
            port = int.Parse(m_MailVO.MailPort);
        }

        m_MailService = new MailService(m_MailVO.MailSmtp, port, enableSSL, m_MailVO.Account, m_MailVO.Password);
    }
 public WebLogService()
 {
     m_SystemFactory = new SystemFactory();
     m_LogService = m_SystemFactory.GetLogService();
 }
 public void TestCaseInit()
 {
     m_AuthFactory = new AuthFactory();
     m_PostFactory = new PostFactory();
     m_SystemFactory = new SystemFactory();
     m_StorageFactory = new StorageFactory();
     m_MemberFactory = new MemberFactory();
     m_AccountingFactory = new AccountingFactory();
     m_AuthService = m_AuthFactory.GetAuthService();
     m_PostService = m_PostFactory.GetPostService();
     m_TemplateService = m_SystemFactory.GetTemplateService();
     m_SystemService = m_SystemFactory.GetSystemService();
     m_MessageService = m_PostFactory.GetMessageService();
     m_StorageFileService = m_StorageFactory.GetStorageFileService();
     m_MemberService = m_MemberFactory.GetMemberService();
     m_AccountingService = m_AccountingFactory.GetAccountingService();
 }
    /// <summary>
    /// DataTable导出到Excel的MemoryStream                                                                      第二步
    /// </summary>
    /// <param name="dtSource">源DataTable</param>
    /// <param name="strHeaderText">表头文本</param>
    public static MemoryStream Export(DataTable dtSource, string strHeaderText, bool usePassword, string path)
    {
        SystemFactory systemFactory = new SystemFactory();
        ISystemService systemService = systemFactory.GetSystemService();
        SystemParamVO systemParamVO = systemService.GetSystemParamByRoot();

        if (string.IsNullOrEmpty(path))
        {
            path = @"D:\temp.xls";
        }

        if (File.Exists(path))
        {
            File.Delete(path);
        }

        HSSFWorkbook fakeWorkbook = new HSSFWorkbook();
        HSSFSheet fakeSheet = fakeWorkbook.CreateSheet() as HSSFSheet;
        FileStream savefile = new FileStream(path, FileMode.Create);
        fakeWorkbook.Write(savefile);
        savefile.Close();
        savefile.Dispose();

        FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
        if (usePassword && systemParamVO!=null && !string.IsNullOrEmpty(systemParamVO.FilePassword))
        {
            NPOI.HSSF.Record.Crypto.Biff8EncryptionKey.CurrentUserPassword = "******";//打开前调用
        }
        HSSFWorkbook workbook = new HSSFWorkbook(file);
        if (usePassword && systemParamVO != null && !string.IsNullOrEmpty(systemParamVO.FilePassword))
        {
            workbook.WriteProtectWorkbook(systemParamVO.FilePassword, "");//设置新密码
        }
        file.Close();

        HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0);

        #region 右击文件 属性信息
        {
            DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
            dsi.Company = "NPOI";
            workbook.DocumentSummaryInformation = dsi;

            SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
            si.Author = "文件作者信息"; //填加xls文件作者信息
            si.ApplicationName = "创建程序信息"; //填加xls文件创建程序信息
            si.LastAuthor = "最后保存者信息"; //填加xls文件最后保存者信息
            si.Comments = "作者信息"; //填加xls文件作者信息
            si.Title = "标题信息"; //填加xls文件标题信息
            si.Subject = "主题信息";//填加文件主题信息

            si.CreateDateTime = DateTime.Now;
            workbook.SummaryInformation = si;
        }
        #endregion

        HSSFCellStyle dateStyle = workbook.CreateCellStyle() as HSSFCellStyle;
        HSSFDataFormat format = workbook.CreateDataFormat() as HSSFDataFormat;
        dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");

        //取得列宽
        int[] arrColWidth = new int[dtSource.Columns.Count];
        foreach (DataColumn item in dtSource.Columns)
        {
            arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length;
        }
        for (int i = 0; i < dtSource.Rows.Count; i++)
        {
            for (int j = 0; j < dtSource.Columns.Count; j++)
            {
                int intTemp = Encoding.GetEncoding(936).GetBytes(dtSource.Rows[i][j].ToString()).Length;
                if (intTemp > arrColWidth[j])
                {
                    arrColWidth[j] = intTemp;
                }
            }
        }
        int rowIndex = 0;
        foreach (DataRow row in dtSource.Rows)
        {
            #region 新建表,填充表头,填充列头,样式
            if (rowIndex == 65535 || rowIndex == 0)
            {
                if (rowIndex != 0)
                {
                    sheet = workbook.CreateSheet() as HSSFSheet;
                }

                #region 表头及样式
                {
                    if (string.IsNullOrEmpty(strHeaderText))
                    {
                        HSSFRow headerRow = sheet.CreateRow(0) as HSSFRow;
                        headerRow.HeightInPoints = 25;
                        headerRow.CreateCell(0).SetCellValue(strHeaderText);
                        HSSFCellStyle headStyle = workbook.CreateCellStyle() as HSSFCellStyle;
                        //headStyle.Alignment = CellHorizontalAlignment.CENTER;
                        HSSFFont font = workbook.CreateFont() as HSSFFont;
                        font.FontHeightInPoints = 20;
                        font.Boldweight = 700;
                        headStyle.SetFont(font);
                        headerRow.GetCell(0).CellStyle = headStyle;
                        sheet.AddMergedRegion(new Region(0, 0, 0, dtSource.Columns.Count - 1));
                        //headerRow.Dispose();
                    }
                }
                #endregion

                #region 列头及样式
                {
                    HSSFRow headerRow = sheet.CreateRow(0) as HSSFRow;
                    HSSFCellStyle headStyle = workbook.CreateCellStyle() as HSSFCellStyle;
                    //headStyle.Alignment = CellHorizontalAlignment.CENTER;
                    HSSFFont font = workbook.CreateFont() as HSSFFont;
                    font.FontHeightInPoints = 10;
                    font.Boldweight = 700;
                    headStyle.SetFont(font);
                    foreach (DataColumn column in dtSource.Columns)
                    {
                        headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
                        headerRow.GetCell(column.Ordinal).CellStyle = headStyle;

                        //设置列宽
                        sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256);
                    }
                    //headerRow.Dispose();
                }
                #endregion

                rowIndex = 1;
            }
            #endregion

            #region 填充内容
            HSSFRow dataRow = sheet.CreateRow(rowIndex) as HSSFRow;
            foreach (DataColumn column in dtSource.Columns)
            {
                HSSFCell newCell = dataRow.CreateCell(column.Ordinal) as HSSFCell;

                string drValue = row[column].ToString();

                switch (column.DataType.ToString())
                {
                    case "System.String"://字符串类型
                        newCell.SetCellValue(drValue);
                        break;
                    case "System.DateTime"://日期类型
                        DateTime dateV;
                        DateTime.TryParse(drValue, out dateV);
                        newCell.SetCellValue(dateV);

                        newCell.CellStyle = dateStyle;//格式化显示
                        break;
                    case "System.Boolean"://布尔型
                        bool boolV = false;
                        bool.TryParse(drValue, out boolV);
                        newCell.SetCellValue(boolV);
                        break;
                    case "System.Int16"://整型
                    case "System.Int32":
                    case "System.Int64":
                    case "System.Byte":
                        int intV = 0;
                        int.TryParse(drValue, out intV);
                        newCell.SetCellValue(intV);
                        break;
                    case "System.Decimal"://浮点型
                    case "System.Double":
                        double doubV = 0;
                        double.TryParse(drValue, out doubV);
                        newCell.SetCellValue(doubV);
                        break;
                    case "System.DBNull"://空值处理
                        newCell.SetCellValue("");
                        break;
                    default:
                        newCell.SetCellValue("");
                        break;
                }
            }
            #endregion

            rowIndex++;
        }
        using (MemoryStream ms = new MemoryStream())
        {
            workbook.Write(ms);
            ms.Flush();
            ms.Position = 0;

            //sheet.Dispose();
            //workbook.Dispose();//一般只用写这一个就OK了,他会遍历并释放所有资源,但当前版本有问题所以只释放sheet
            return ms;
        }
    }