的.xlsx创建和保存的EPPlus使用模板是不可读/损坏 [英] .xlsx Created and Saved Using Template with EPPlus is Unreadable/Corrupt

查看:1493
本文介绍了的.xlsx创建和保存的EPPlus使用模板是不可读/损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建使用的 Excel模板的创建的 Excel文档<一个小程序/ em>的,然后使用 EPPlus 写几个单元。不幸的是,这些文件似乎已损坏,无论我怎么努力

I'm trying to create a small program that uses an Excel template to create an Excel document and then writes to several cells using EPPlus. Unfortunately, the files appear to be corrupt no matter what I try.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OfficeOpenXml;
using System.IO;

namespace ConsoleApplication9
{
    public sealed class ExcelSerialize
    {
        private readonly List<Tuple<string, string>> Results;
        private readonly string Directory;
        private ExcelPackage package;

        public ExcelSerialize(List<Tuple<string, string>> Results, string Directory)
        {
            this.Results = Results;
            this.Directory = Directory;
        }

        public bool WriteResults()
        {
            FileInfo template = new FileInfo(Directory);

            using (package = new ExcelPackage(template, true))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets[1];

                //foreach (Tuple<string, string> Result in Results)
                //{
                //    worksheet.Cells[Result.Item1].Value = Result.Item2;
                //}

                string file = string.Format(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"results\results" + System.DateTime.Now.ToString().Replace(" ", "").Replace("/", "_").Replace(":", "-") + ".xlsx");

                Byte[] bin = package.GetAsByteArray();
                File.WriteAllBytes(file, bin);

                return true;
            }
        }
    }
}



的东西我试过:




  • 在模板中改变各种单元格的值

  • 保存的。的 Excel中的文件从该模板创建无需编写任何新的数据到它。

  • 创建与单元格A1,A2和A3含有TEST一个基本的模板,没有其他的编辑,而不是更复杂的模板,我打算使用。

  • 保存使用 Package.SaveAs()

  • 使用保存在示例代码中看到的字节数组。

  • 从提供Codeplex上的最新源代码编译EPPlus。

  • Things I've tried:

    • Changing the values of various cells in the template.
    • Saving an Excel document created from the template without writing any new data to it.
    • Creating a basic template with the cells A1, A2, and A3 containing "TEST" and no other edits instead of the more complicated template I intend to use.
    • Saving using Package.SaveAs().
    • Saving using the Byte array seen in the example code.
    • Compiling EPPlus from the latest source provided on Codeplex.

      • 使用下面的代码创建一个新的文件:

      using (package = new ExcelPackage(string.Format(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"results\results" + System.DateTime.Now.ToString().Replace(" ", "").Replace("/", "_").Replace(":", "-") + ".xlsx"))
      {
          ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Test");
          worksheet.Cells[A1].Value = "Test";
          package.Save();
      }
      



      注:



      无论出于何种原因,仍然保存文件出现损坏,无法恢复。我目前使用的的Microsoft Office 2010 的。我使用的文件格式为 .xltx 的.xlsx

      Notes:

      For whatever reason, the files saved still appear corrupt and can't be recovered. I'm currently using Microsoft Office 2010. The file formats I'm using are .xltx and .xlsx.

      推荐答案

      EPPlus库不从现有的XLTX Excel模板支持文件的创建。

      Short Answer:

      The EPPlus library does not support creation of a file from an existing xltx Excel template.

      使用预格式化XLSX文件,而不是

      Use a pre-formatted xlsx file instead.

      Dim excelFile As New FileInfo(filename)
      Dim reportTemplate As New FileInfo(templatePath)
      Dim xlFile As ExcelPackage = New ExcelPackage(excelFile, reportTemplate)
      
      ' Do Stuff
      
      xlFile.Save()
      

      在使用EPPlus你应该能够与XLTX文件我一直得到一个损坏的输出文件时提供给它提供一个模板作为第二个参数(见上面的代码片段),但实例化一个新ExcelPackage对象与上述相同的用户。我最终发现,提供一个普通的xlsx文件作为模板,而不是一个实际的模板文件,出现了工作。

      When instantiating a new ExcelPackage object using EPPlus you're supposed to be able to provide a template as the second parameter (see code snippet above), however when providing it with an xltx file I kept getting a corrupt output file same as the user above. I eventually found that supplying a regular xlsx file as the template, rather than an actual template file, appeared to work.

      这是一个开放源码包,所以我决定快速浏览一下在源。

      This is an open source package, so I decided to take a quick look at the source.

      它看起来像ExcelPackage构造函数接受模板参数,调用CreateFromTemplate()方法。在CreateFromTemplate()方法,该意见指出,它预计的现有XLSX 文件作为模板,而不是实际的模板文件中使用。

      It looks like the ExcelPackage constructors that accept the "template" parameter, call the CreateFromTemplate() method. The comments on the CreateFromTemplate() method states that it expects an existing xlsx file to use as a template, not an actual template file.

      因此,虽然人们可能会认为的模板参数指的是实际的XLTX模板文件,看来EPPlus不支持。

      So while one might assume that the 'template' parameter refers to an actual xltx template file, it appears that EPPlus does not support that.

      这篇关于的.xlsx创建和保存的EPPlus使用模板是不可读/损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆