Esempio n. 1
0
        /// <summary>
        /// 打开系统对话框选择一个文件。
        /// </summary>
        /// <returns></returns>
        public static System.IO.FileInfo SelectFile()
        {
            System.IO.FileInfo theFile = null;
            // 在WPF中, OpenFileDialog位于Microsoft.Win32名称空间
            OpenFileDialog dialog = new OpenFileDialog
            {
                Filter = "工作文档|*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx;*.wps;*.pdf;*.jpg;*.jpeg;*.png;*.gif;|压缩文档|*.rar;*.zip|所有文件|*.*"
            };

            if (dialog.ShowDialog() == true)
            {
                try
                {
                    theFile = new System.IO.FileInfo(dialog.FileName);
                    if (theFile.Length > 1073741824)//1G
                    {
                        AppFuns.ShowMessage("文件大于1G,无法保存!");
                        return(null);
                    }
                }
                catch (Exception Error)
                {
                    AppFuns.ShowMessage($"读取文件出错(正在使用?){Error.Message}");
                    return(null);
                }
            }
            return(theFile);
        }
Esempio n. 2
0
        /// <summary>
        /// NPOI导出Excel,不依赖本地是否装有Excel,导出速度快
        /// </summary>
        /// <param name="dataGridView1">要导出的dataGridView控件</param>
        /// <param name="sheetName">sheet表名</param>
        private void ExportToExcel(List <Lib.MemberInfoEntity> EntityList, string sheetName)
        {
            if (EntityList == null || EntityList.Count < 1)
            {
                return;
            }
            System.Windows.Forms.SaveFileDialog fileDialog = new System.Windows.Forms.SaveFileDialog();
            fileDialog.Filter = "Excel|*.xls";
            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            //不允许dataGridView显示添加行,负责导出时会报最后一行未实例化错误
            HSSFWorkbook workbook = new HSSFWorkbook();
            ISheet       sheet    = workbook.CreateSheet(sheetName);
            IRow         rowHead  = sheet.CreateRow(0);

            PropertyInfo[] EntityProps = EntityList[0].GetType().GetProperties();
            //填写表头
            for (int i = 0; i < EntityProps.Count(); i++)
            {
                rowHead.CreateCell(i, CellType.String).SetCellValue(EntityProps[i].Name);
            }
            //填写内容
            for (int i = 0; i < EntityList.Count; i++)
            {
                IRow row = sheet.CreateRow(i + 1);
                for (int j = 0; j < EntityProps.Count(); j++)
                {
                    object TempObj = EntityProps[j].GetValue(EntityList[i]);
                    if (TempObj != null)
                    {
                        row.CreateCell(j, CellType.String).SetCellValue(EntityProps[j].GetValue(EntityList[i]).ToString());
                    }
                    else
                    {
                        row.CreateCell(j, CellType.String).SetCellValue("");
                    }
                }
            }

            using (FileStream stream = File.OpenWrite(fileDialog.FileName))
            {
                workbook.Write(stream);
                stream.Close();
            }
            AppFuns.ShowMessage("导出数据成功!", "提示");
            GC.Collect();
        }