1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using OfficeOpenXml;
...
{
if (!string.IsNullOrEmpty(filename))
{
// Incorporated resource
var fic = Path.Combine(Path.GetTempPath(), "empty_template_prev_journ.xlsx");
try
{
using (var streamReader = new BinaryReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Chemin du template")))
{
File.WriteAllBytes(fic, streamReader.ReadBytes((int)streamReader.BaseStream.Length));
}
// Taking existing file: 'Sample1.xlsx'. Here 'Sample1.xlsx' is treated as template file
//string resTempFile = @"D:\Sopra\workspace_Visual_Studio\Evpt\EVPT\EVPT v3\HeavyClient\AtosOrigin.Airbus.Evpt.UI\empty_template_prev_journ.xlsx";
// Access to the new file
using (var streamDest = new FileStream(filename, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete | FileShare.Inheritable))
{
// Acces to the template
using (var streamTemp = new FileStream(fic, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete | FileShare.Inheritable))
{
// Create the package from the new file and the template
using (ExcelPackage package = new ExcelPackage(streamDest, streamTemp))
{
// Openning first Worksheet of the template file i.e. 'Sample1.xlsx'
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
// Fill the excel file
worksheet.InsertRow(29, 1, 28);
worksheet.SetValue(28, 1, "Toto");
worksheet.SetValue(28, 2, "Pouet");
worksheet.SetValue(29, 1, "Titi");
worksheet.SetValue(29, 2, "51");
// Save the excel file
package.Save();
}
}
}
}
finally
{
if (File.Exists(fic))
{
File.Delete(fic);
}
}
}
} |
Partager