Surement quelque chose là-bas. J'ai été capable de le reproduire moi-même. Cela a à voir avec la table que vous avez créée. Si vous ouvrez votre fichier et le supprimez à l'aide de l'option «Convertir en plage» dans l'onglet Outils de table, le problème disparaît.
J'ai regardé le code source et il extrait les fichiers xml au niveau du zip et n'a vu aucune indication qu'il était en train de jouer avec eux - semblait être une copie droite.
Très étrange car si nous créons et sauvegardons le fichier xlsx incluant une table de EPPlus le problème n'est pas là. Cela fonctionne très bien:
[TestMethod]
public void Template_Copy_Test()
{
//http://stackoverflow.com/questions/28722945/epplus-with-a-template-is-not-working-as-expected
const string templatePath = "c:\\temp\\testtemplate.xlsx"; // the path of the template
const string resultPath = "c:\\temp\\result.xlsx"; // the path of our result
//Throw in some data
var dtdata = new DataTable("tblData");
dtdata.Columns.Add(new DataColumn("Col1", typeof(string)));
dtdata.Columns.Add(new DataColumn("Col2", typeof(int)));
dtdata.Columns.Add(new DataColumn("Col3", typeof(int)));
for (var i = 0; i < 20; i++)
{
var row = dtdata.NewRow();
row["Col1"] = "String Data " + i;
row["Col2"] = i * 10;
row["Col3"] = i * 100;
dtdata.Rows.Add(row);
}
var templateFile = new FileInfo(templatePath);
if (templateFile.Exists)
templateFile.Delete();
using (var pck = new ExcelPackage(templateFile))
{
var ws = pck.Workbook.Worksheets.Add("Data");
ws.Cells["A1"].LoadFromDataTable(dtdata, true);
for (var i = 2; i <= dtdata.Rows.Count + 1; i++)
ws.Cells[i, 4].Formula = String.Format("{0}*{1}", ExcelCellBase.GetAddress(i, 2), ExcelCellBase.GetAddress(i, 3));
ws.Tables.Add(ws.Cells[1, 1, dtdata.Rows.Count + 1, 4], "TestTable");
pck.Save();
}
using (var pck = new ExcelPackage(new FileInfo(resultPath), templateFile)) // creating a package with the given template, and our result as the new stream
{
// note that I am not doing any work ...
pck.Save(); // savin our work
}
}
MAIS .....
Si nous ouvrons testtemplate.xlsx
, supprimer la table, sauvegarder/fermer le fichier, ouvrez à nouveau, et réintroduisez la même table exacte montre les problèmes lorsque vous exécutez ceci:
[TestMethod]
public void Template_Copy_Test2()
{
//http://stackoverflow.com/questions/28722945/epplus-with-a-template-is-not-working-as-expected
const string templatePath = "c:\\temp\\testtemplate.xlsx"; // the path of the template
const string resultPath = "c:\\temp\\result.xlsx"; // the path of our result
var templateFile = new FileInfo(templatePath);
using (var pck = new ExcelPackage(new FileInfo(resultPath), templateFile)) // creating a package with the given template, and our result as the new stream
{
// note that I am not doing any work ...
pck.Save(); // savin our work
}
}
Il doit être quelque chose dans leurs méthodes burried de copie zip mais je ne m'a sauté aux yeux.
Mais au moins, vous pouvez voir à travailler autour de lui.
Ernie
Quelle version de EpPlus utilisez-vous?Cela ressemble à un numéro d'EpPlus et peut-être à être publié dans [la section correspondante du projet] (http://epplus.codeplex.com/workitem/list/basic). – Deilan
J'utilise la version 4.0.3 –
Ouvrez le fichier modèle avec Excel 2007+ et enregistrez-le sous un autre nom de fichier. Ensuite, utilisez le nouveau fichier en utilisant le même flux de travail, que vous avez décrit dans la publication. Est-ce qu'il se reproduit? – Deilan