Exemplo n.º 1
0
        //#region 文件操作
        /// <summary>
        /// 通常是新增页面的文件上传,保存至Session中
        /// </summary>
        /// <param name="sessionId">session的ID</param>
        /// <param name="ManID">上传人的ID</param>
        /// <param name="fileFolderName">上传文件的文件夹名次</param>
        /// <param name="fu">上传控件</param>
        /// <param name="listbox">ListBox显示控件</param>
        public static void SessionAddFile(string sessionId, string ManID, string fileType, string fileFolderName, FileUpload fu, ListBox listbox, string ids)
        {
            List <ModelFile> list = null;

            //文件名称
            string fileName = Path.GetFileName(fu.PostedFile.FileName);

            //创建并且返回文件夹路径
            string filepath = UploadFileCommon.CreateDir(fileFolderName);

            //文件大小
            int fileContentLength = fu.PostedFile.ContentLength;

            //文件扩展名
            string fileExtension = fu.PostedFile.FileName.Substring(fu.PostedFile.FileName.LastIndexOf('.') + 1);

            if (HttpContext.Current.Session[sessionId] == null)
            {
                list = new List <ModelFile>();
            }
            else
            {
                list = HttpContext.Current.Session[sessionId] as List <ModelFile>;
            }

            ModelFile model = new ModelFile();

            model.File_ID = Guid.NewGuid().ToString();

            model.File_Name = fileName;

            model.File_Extension = fileExtension;

            model.File_Size = fileContentLength;

            model.File_Path = filepath + UploadFileCommon.CreateFileName(fileExtension);

            //model.File_Date = DateTime.Now;

            //model.File_CurrentMan = ManID;

            model.File_Type = fileType;

            fu.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(model.File_Path));

            list.Add(model);

            HttpContext.Current.Session[sessionId] = list;

            listbox.Items.Add(new ListItem(fileName, model.File_ID));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 纪检新闻中验证上传新闻图片格式
        /// </summary>
        /// <param name="fu"></param>
        /// <param name="filelength"></param>
        /// <param name="chkExtension"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static bool CheckNewsFile(FileUpload fu, int filelength, string chkExtension, out string msg)
        {
            string fileName = Path.GetFileName(fu.PostedFile.FileName);

            if (chkExtension == "")
            {
                chkExtension = "jpg|gif|bmp|jpeg|png";
            }

            if (!string.IsNullOrEmpty(fileName))
            {
                //文件扩展名
                string fileExtension = UploadFileCommon.GetFileExtension(fu.PostedFile.FileName);

                //文件大小
                int fileContentLength = fu.PostedFile.ContentLength;

                if (!CheckFileLength(fileContentLength, 30))
                {
                    msg = "您上传的文件过大,请重新上传!";

                    return(false);
                }

                if (!CheckExtension(fileExtension, chkExtension))
                {
                    msg = "您上传的文件类型不正确,请重新上传!";

                    return(false);
                }
            }
            else
            {
                msg = "请选择上传文件!";

                return(false);
            }

            msg = "";

            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 针对编辑页面,返回一个NewFile实体
        /// </summary>
        /// <param name="fu">file控件</param>
        /// <param name="fileFolderName">文件夹名称</param>
        /// <param name="ParentID">对应的主表ID</param>
        /// <param name="ManID">当前上传人</param>
        /// <returns></returns>
        public static ModelFile  AddFile(FileUpload fu, string fileFolderName, string ParentID, string ManID, string fileType, DateTime addtime)
        {
            ModelFile model = new ModelFile();
            //文件名称
            string fileName = fu.FileName;

            //创建并且返回文件夹路径
            string filepath = UploadFileCommon.CreateDir(fileFolderName);

            //文件大小
            int fileContentLength = fu.PostedFile.ContentLength;

            //文件扩展名
            string fileExtension = fu.PostedFile.FileName.Substring(fu.PostedFile.FileName.LastIndexOf('.') + 1);

            //DB.NewsFile model = new DB.NewsFile();

            model.File_ID = Guid.NewGuid().ToString();

            model.File_Name = fileName;

            model.File_Extension = fileExtension;

            model.File_Size = fileContentLength;

            model.File_Path = filepath + UploadFileCommon.CreateFileName(fileExtension);

            //model.CreateDate = DateTime.Now;

            //model.addTime = addtime;

            //model.news_id = ParentID;

            model.File_Type = fileType;

            //model.CreateUser = ManID;

            fu.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(model.File_Path));

            return(model);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 导出数据到本地
        /// </summary>
        /// <param name="dt">要导出的数据</param>
        /// <param name="WorkBookName">导出的Excel的工作簿名称</param>
        public static string OutFileToDisk(DataTable dt, string WorkBookName, bool IsDownload = true)
        {
            Workbook  workbook = new Workbook();         //工作簿
            Worksheet sheet    = workbook.Worksheets[0]; //工作表

            sheet.Name = WorkBookName;
            Cells cells = sheet.Cells;//单元格

            //为标题设置样式
            Style styleTitle = workbook.Styles[workbook.Styles.Add()]; //新增样式

            styleTitle.HorizontalAlignment = TextAlignmentType.Center; //文字居中
            styleTitle.Font.Name           = "宋体";                     //文字字体
            styleTitle.Font.Size           = 18;                       //文字大小
            styleTitle.Font.IsBold         = true;                     //粗体

            //样式2
            Style style2 = workbook.Styles[workbook.Styles.Add()]; //新增样式

            style2.HorizontalAlignment = TextAlignmentType.Center; //文字居中
            style2.Font.Name           = "宋体";                     //文字字体
            style2.Font.Size           = 14;                       //文字大小
            style2.Font.IsBold         = true;                     //粗体
            //style2.IsTextWrapped = true;//单元格内容自动换行
            style2.Borders[BorderType.LeftBorder].LineStyle   = CellBorderType.Thin;
            style2.Borders[BorderType.RightBorder].LineStyle  = CellBorderType.Thin;
            style2.Borders[BorderType.TopBorder].LineStyle    = CellBorderType.Thin;
            style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

            //样式3
            Style style3 = workbook.Styles[workbook.Styles.Add()]; //新增样式

            style3.HorizontalAlignment = TextAlignmentType.Center; //文字居中
            style3.Font.Name           = "宋体";                     //文字字体
            style3.Font.Size           = 12;                       //文字大小
            style3.Borders[BorderType.LeftBorder].LineStyle   = CellBorderType.Thin;
            style3.Borders[BorderType.RightBorder].LineStyle  = CellBorderType.Thin;
            style3.Borders[BorderType.TopBorder].LineStyle    = CellBorderType.Thin;
            style3.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;

            int Colnum = dt.Columns.Count; //表格列数
            int Rownum = dt.Rows.Count;    //表格行数

            ////生成行1 标题行
            //cells.Merge(0, 0, 1, Colnum);//合并单元格
            //cells[0, 0].PutValue(tableName);//填写内容
            //cells[0, 0].SetStyle(styleTitle);
            //cells.SetRowHeight(0, 38);

            //生成行2 列名行
            for (int i = 0; i < Colnum; i++)
            {
                cells[0, i].PutValue(dt.Columns[i].ColumnName);
                cells[0, i].SetStyle(style2);
                cells.SetColumnWidth(i, 20);
                cells.SetRowHeight(0, 25);
            }

            //生成数据行
            for (int i = 0; i < Rownum; i++)
            {
                for (int k = 0; k < Colnum; k++)
                {
                    cells[1 + i, k].PutValue(dt.Rows[i][k].ToString());
                    cells[1 + i, k].SetStyle(style3);
                }
                cells.SetRowHeight(1 + i, 24);
            }

            string tempPath = UploadFileCommon.CreateDir("EXL");

            if (!Directory.Exists(tempPath))//查看当前文件夹是否存在
            {
                Directory.CreateDirectory(tempPath);
            }
            try
            {
                // string sNewFileName = DateTime.Now.ToString("yyyyMMddhhmmsfff");//上传后的文件名字
                string sNewFileName = WorkBookName;
                string ph           = tempPath + @"/" + sNewFileName + ".xlsx";
                workbook.Save(System.Web.HttpContext.Current.Request.MapPath(ph));
                if (IsDownload)
                {
                    ExportToExcel(ph);//导出Excel
                }
                return(ph);
            }
            catch { return("0"); }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 组件导出Excel
        /// 返回的是文件路径,如果返回是“0”导出失败(失败:1、DataTable为null或者没有数据,2、没有Excel组件,3、文件存放错误)
        /// </summary>
        /// <param name="dt">数据DataTable</param>
        /// <param name="WorkBookName">导出的Excel的工作簿名称</param>
        /// <returns></returns>
        public string ExportExcels(DataTable dt, string WorkBookName)
        {
            if (dt == null || dt.Rows.Count == 0)
            {
                return("0");
            }
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

            if (xlApp == null)
            {
                return("0");
            }
            System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook  workbook  = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];
            worksheet.Name = WorkBookName;
            Microsoft.Office.Interop.Excel.Range range;
            range = worksheet.Range["A1", worksheet.Cells[1, dt.Columns.Count]];
            long  totalCount = dt.Rows.Count;
            long  rowRead    = 0;
            float percent    = 0;

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                worksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
                range = (Microsoft.Office.Interop.Excel.Range)worksheet.Cells[1, i + 1];
                range.Interior.ColorIndex = 15;
                range.Font.Bold           = true;
            }
            for (int r = 0; r < dt.Rows.Count; r++)
            {
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    worksheet.Cells[r + 2, i + 1] = dt.Rows[r][i].ToString();
                }
                rowRead++;
                percent = ((float)(100 * rowRead)) / totalCount;
            }
            //xlApp.Visible = true;
            range.NumberFormatLocal = "@";
            workbook.Saved          = true;
            string tempPath = UploadFileCommon.CreateDir("EXL");

            if (!Directory.Exists(tempPath))//查看当前文件夹是否存在
            {
                Directory.CreateDirectory(tempPath);
            }
            try
            {
                string sNewFileName = DateTime.Now.ToString("yyyyMMddhhmmsfff");//上传后的文件名字
                string ph           = tempPath + @"/" + sNewFileName + ".xlsx";
                workbook.SaveAs(System.Web.HttpContext.Current.Request.MapPath(ph));
                return(ph);
            }
            catch { return("0"); }
            finally
            {
                workbook.Close(true, Type.Missing, Type.Missing);
                workbook = null;
                xlApp.Quit();
                xlApp = null;
            }
        }