Generating a new file with a template in Excel when saving

1

Good morning, my software works with bar code reading for generating printed labels. It has several Texbox, each one receiving from the reader a different code. When I click on Save it sends these codes to an Excel worksheet where I can print and attach in the box with the products. Only I could do that every time I clicked Save, it overwrites a template, in this case for formatting and name of the company are organized, but when there is a "BO" error, instead of looking for the file in xls referring to those tags, it is necessary to re-beep because the file does not exist, since it will always overwrite the previous one, Click Save, and every time it generates a new file, referencing it with the number of the box that is filled in a texbox?

//Criando arquivo no Excel.exe:
        Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel.Workbook sheet = Excel.Workbooks.Open("C:\Users\Core i3\Desktop\EtiqMasterBoxTornozeleira.xlsx");
        Microsoft.Office.Interop.Excel.Worksheet x = Excel.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;
        Excel.Range userRanger = x.UsedRange;

        int countRecords = userRanger.Rows.Count;
        int add = countRecords + 1;
    
asked by anonymous 18.11.2016 / 16:47

1 answer

1

Use the SaveAs () of Excel.Workbook to save under another name, so you will only use the EtiqMasterBox Ankle.xlsx as a template.

It will look something like this:

sheet.SaveAs(string.Format("C:\Users\Core i3\Desktop\{0}.xlsx", suaEtiqueta));
    
18.11.2016 / 17:07