CreateExcel() public static method

public static CreateExcel ( string path ) : Excel,
path string
return Excel,
Esempio n. 1
0
        public void GenerateExcel(List <ExcelData> sheets, string fileName)
        {
            var helper = new ExcelHelper();

            if (sheets.Count == 1)
            {
                ExcelData sheet = sheets[0];
                using (var stream = helper.CreateExcel(sheet.DataRows, sheet.SheetName, sheet.Headers))
                {
                    using (FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                    {
                        stream.WriteTo(file);
                        file.Close();
                        stream.Close();
                    }
                }
            }
            else
            {
                using (var stream = helper.CreateExcelWithManySheets(sheets))
                {
                    using (FileStream file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
                    {
                        stream.WriteTo(file);
                        file.Close();
                        stream.Close();
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 模板下载
        /// </summary>
        /// <param name="code"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public byte[] GetbytesByCode(string code, out string name)
        {
            var impTb = _impTbRepository.GetAllList(a => a.Code == code).FirstOrDefault();

            name = impTb.CnTableName;
            var impTbField = impTb.ImpTbField;
            var dic        = new Dictionary <string, string>();

            if (impTbField.Count > 0)
            {
                foreach (var item in impTbField)
                {
                    dic.Add(item.FieldCode, item.FieldName);
                }
            }

            var sheet = new List <MultiSheet>();

            sheet.Add(new MultiSheet {
                SheetName = "ss", Data = null, Description = null, TopTitle = null, DicTitle = dic
            });
            var ss = ExcelHelper.CreateExcel(sheet, "ssss", name);

            return(ss.GetBuffer());
        }
Esempio n. 3
0
    protected void Page_Load(object sender, EventArgs e)
    {
        int         index      = 1;
        IEnumerable enumerable = AdminService.UserService.Search().Select(u => new
        {
            id   = index++,
            name = u.UserName,
            pwd  = u.UserPwd,
            date = u.AddDate
        }).ToList();

        var templateFileName = Server.MapPath("~/template.xls");
        var excelHelper      = new ExcelHelper(3, 0, templateFileName);

        var workbook = excelHelper.CreateExcel(enumerable);
        var ms       = new MemoryStream();

        workbook.Write(ms);

        Response.Charset         = "UTF-8";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
        Response.ContentType     = "application/vnd.ms-excel";

        string fileName = "会员信息列表.xls";

        Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileName));
        Response.BinaryWrite(ms.GetBuffer());
        Response.Flush();
        Response.End();
    }
Esempio n. 4
0
        static void Main(string[] args)
        {
            List <Po> pos = new List <Po>();

            for (int t = 0; t <= 25; t++)
            {
                for (int j = 1; j <= 5; j++)
                {
                    GNN4PO po1 = new GNN4PO();
                    po1.CalculateA(t);
                    pos.Add(new Po {
                        Column = t + 1, Row = j, Value = po1.a
                    });
                    po1 = null;
                }

                Console.WriteLine(string.Format("i={0}, a1-a5:\r\n{1},{2},{3},{4},{5}", t, pos[5 * t + 0].Value, pos[5 * t + 1].Value, pos[5 * t + 2].Value, pos[5 * t + 3].Value, pos[5 * t + 4].Value));


                //GNN4PO po1 = new GNN4PO();
                //po1.CalculateA(t);
                //GNN4PO po2 = new GNN4PO();
                //po2.CalculateA(t);
                //GNN4PO po3 = new GNN4PO();
                //po3.CalculateA(t);
                //GNN4PO po4 = new GNN4PO();
                //po4.CalculateA(t);
                //GNN4PO po5 = new GNN4PO();
                //po5.CalculateA(t);

                //Console.WriteLine(string.Format("i={0}, a1-a5:\r\n{1},{2},{3},{4},{5}", t, po1.a, po2.a, po3.a, po4.a, po5.a));
                //pos.Add(new Po { Column = t + 1, Row = 1, Value = po1.a });
                //pos.Add(new Po { Column = t + 1, Row = 2, Value = po2.a });
                //pos.Add(new Po { Column = t + 1, Row = 3, Value = po3.a });
                //pos.Add(new Po { Column = t + 1, Row = 4, Value = po4.a });
                //pos.Add(new Po { Column = t + 1, Row = 5, Value = po5.a });
            }
            Console.ReadLine();

            ExcelHelper helper = new ExcelHelper();

            helper.CreateExcel(pos);
        }
Esempio n. 5
0
        public ActionResult ExcelExport(string formvar)
        {
            ListCondition condition = new ListCondition();

            condition.SortField       = DataConvert.ToString(Request.Params["sidx"]);
            condition.SortType        = DataConvert.ToString(Request.Params["sord"]);
            condition.PageIndex       = DataConvert.ToInt32(Request.Params["page"]);
            condition.PageRowNum      = DataConvert.ToInt32(Request.Params["rows"]);
            condition.ListModelString = formvar;
            DataTable           dt     = Repository.GetReportGridDataTable(condition, false);
            QueryEntryViewModel model  = JsonHelper.Deserialize <QueryEntryViewModel>(formvar);
            StringBuilder       sbHtml = ExcelHelper.CreateExcel(dt, EntryGridLayout(model.FormMode));

            //string fileName = Server.MapPath("~/Content/uploads/output.xls");
            //ExcelHelper.CreateExcel(dt, EntryGridLayout(), "main", "报表", fileName);
            //return File(fileName, "application/ms-excel", IdGenerator.GetMaxId("ExcelExport") + ".xls");
            byte[] fileContents = Encoding.UTF8.GetBytes(sbHtml.ToString());
            return(File(fileContents, "application/ms-excel", IdGenerator.GetMaxId(ExportFileName) + ".xls"));
        }
        private void button3_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Filter   = "导出Excel(*.xls)|*.xls";
            sfd.FileName = "" + reportobj.REPORT_DISPLAYNAME + "" + DateTime.Now.ToString("yyyyMMddhhmmss");
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                Cursor.Current = Cursors.WaitCursor;
                bool res = ExcelHelper.CreateExcel(dgvDiscourseList, sfd.FileName, @"" + (reportobj.REPORT_XLS == "publicXls" ? "" : reportobj.REPORT_XLS) + "");
                if (res)
                {
                    MessageBox.Show("导出成功!");
                }
                else
                {
                    MessageBox.Show("导出失败!");
                }
                Cursor.Current = Cursors.Default;
            }
        }
Esempio n. 7
0
 public ActionResult Export(string formvar)
 {
     try
     {
         ListCondition condition = new ListCondition();
         condition.SortField       = DataConvert.ToString(Request.Params["sidx"]);
         condition.SortType        = DataConvert.ToString(Request.Params["sord"]);
         condition.PageIndex       = DataConvert.ToInt32(Request.Params["page"]);
         condition.PageRowNum      = DataConvert.ToInt32(Request.Params["rows"]);
         condition.ListModelString = formvar;
         UserInfo sysUser = CacheInit.GetUserInfo(HttpContext);
         condition.SysUser = sysUser;
         DataTable     dt           = ListRepository.GetAllGridDataTable(condition);
         StringBuilder sbHtml       = ExcelHelper.CreateExcel(dt, GridLayout());
         byte[]        fileContents = Encoding.UTF8.GetBytes(sbHtml.ToString());
         return(File(fileContents, "application/ms-excel", IdGenerator.GetMaxId(ExportFileName) + ".xls"));
     }
     catch (Exception ex)
     {
         AppLog.WriteLog(AppMember.AppText["SystemUser"], LogType.Error, "MasterController.Export", ControllerName + "[Message]:" + ex.Message + " [StackTrace]:" + ex.StackTrace);
         return(Content(ControllerName + "[Message]:" + ex.Message + " [StackTrace]:" + ex.StackTrace, "text/html"));
     }
 }
Esempio n. 8
0
        public static MvcHtmlString AppReportPrintFor(this HtmlHelper htmlHelper, DataTable dt, AdvanceGridLayout layout)
        {
            StringBuilder sb = ExcelHelper.CreateExcel(dt, layout);

            return(MvcHtmlString.Create(sb.ToString()));
        }
Esempio n. 9
0
        public async Task <FileStreamResult> TranslateExcelAsync(IFormCollection collection)
        {
            var files = Request.Form.Files;



            IList <IList <string> > resultList = new List <IList <string> >();

            var filePath = @"data/excel/";

            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            IList <string> list = new List <string>();

            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    string fileFullPath = filePath + DateTime.Now.Millisecond + formFile.FileName;

                    using (var stream = new FileStream(fileFullPath, FileMode.Create))
                    {
                        list.Add(fileFullPath);
                        await formFile.CopyToAsync(stream);
                    }
                }
            }
            foreach (var item in list)
            {
                var excelData = _excelHelper.ReadExcel(item);

                //此处只读第一列

                IList <string> textList = new List <string>();
                foreach (var text in excelData)
                {
                    textList.Add(text[0]);     //只读第一列
                }
                resultList.Add(textList);


                List <Task> taskList = new List <Task>();

                var task2 = _translator.Translateasync("en", "fr", textList);  //翻译成法语
                var task1 = _translator.Translateasync("en", "de", textList);  //翻译成德文

                await Task.WhenAll(task1, task2);

                resultList.Add(task2.Result);
                resultList.Add(task1.Result);
                string savePath = $"data/{DateTime.Now.ToFileTime()}.xlsx";
                _excelHelper.CreateExcel(resultList, savePath);


                FileStream fileStream = new FileStream(savePath, FileMode.Open);
                return(File(fileStream, "text/comma-separated-values", "result" + DateTime.Now.ToFileTime() + ".xlsx"));
            }
            return(null);
            // process uploaded files
            // Don't rely on or trust the FileName property without validation.

            //    return Ok(new { result });
        }