コード例 #1
0
    public static byte[] GenerateExcelV2(ExcelData data)
    {
        try
        {
            var root = HttpContext.Current.Server.MapPath("~");

            using (MemoryStream file = new MemoryStream())
            {
                if (data.IsEn)
                {
                    if (TemplateFileEn == null)
                    {
                        TemplateFileEn = new MemoryStream();

                        using (var stream = File.Open(root + TemplateFileEnPath, FileMode.Open))
                        {
                            stream.CopyTo(TemplateFileEn);
                        }
                    }

                    TemplateFileEn.Position = 0;
                    TemplateFileEn.CopyTo(file);
                }
                else
                {
                    if (TemplateFile == null)
                    {
                        TemplateFile = new MemoryStream();

                        using (var stream = File.Open(root + TemplateFilePath, FileMode.Open))
                        {
                            stream.CopyTo(TemplateFile);
                        }
                    }

                    TemplateFile.Position = 0;
                    TemplateFile.CopyTo(file);
                }

                var result = UpdateCells(file, data.Title, data.Headers, data.Rows);
            }

            return(result);
        }
        catch (Exception exception)
        {
            //todo: log in db
        }

        return(null);
    }