Generating a New File with an Excel template when Saving

Asked

Viewed 328 times

1

Good morning, My software works with barcode reading for generation of printed labels. It has several Texbox , each receiving a different code from the reader. By clicking on Save it sends these codes to an Excel spreadsheet where I can print and attach them in the box with the products.Only I only managed to get every time it was clicked on Save, it overwritten a template , in case for formatting and company name come out organized, only when there is error "B.O", when sending to search 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, have to Click Save , and every time it generates a new file, referencing with the box number that is populated 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;

1 answer

1


Use the Saveas() Workbook to save with another name, so you will only use Etiqmasterboxtornozeleira.xlsx as template.

It’s gonna be something like this:

sheet.SaveAs(string.Format("C:\\Users\\Core i3\\Desktop\\{0}.xlsx", suaEtiqueta));
  • what I would have to pass as information in this field "yourSet" ?

  • @Luizfelipe, in this place you will be passing the unique name of your file, the name that will be created the .xlsx. Remembering that it has to be a string =]

  • One more question, I put the code up so: Microsoft.Office.Interop.Excel.Workbook sheet = sheet.SaveAs(string.Format("C:\\Users\\Core i3\\Desktop\\{0}.xlsx", "tornozeleira")); would be right that way ? , here it presented error @Brunno, I tried to pass the name of my file in excel but it shows error.

  • @Luizfelipe, the Saveas() method is void, returns nothing. The correct is after you finish everything, have set the values call it to generate the file. The call must be equal to shown above =]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.