How to write an . xlsx file permanently, using the Closedxml library?

Asked

Viewed 20 times

0

When you run the code for simulation, you notice that it is changing the file test.xlsx as expected, however, when closing the execution it does not save the changes as, when running the program again it will start a new file and will not pull the already changed base.



public partial class Form1 : Form
{
    
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        using (var workbook = new XLWorkbook())
        {
            string DataEmprestimo;
            string DataDevolucao;
            var row = 1;
            var col = 1;
            var linha = row;
            var coluna = col;
            int cont = 2;

            var planilha = workbook.Worksheets.Add("Planilha1");

            planilha.Cell(linha, coluna).Value = "Equipamento";
            var range = planilha.Range("A1"); 
            range.Merge().Style.Font.SetBold().Font.FontSize = 12;
            planilha.Columns().AdjustToContents();

            linha = linha + 1;

            for (cont = 2; cont <= 20000; cont++)
            {
                var Equipamento = planilha.Cell("A" + cont.ToString()).Value.ToString();
                if (string.IsNullOrEmpty(Equipamento) == true)
                {
                    linha = cont;
                    planilha.Cell(linha, coluna).Value = textBox1.Text;
                    
                    break;
                }
            }
            /*
            while (true)
            {
                var Equipamento = planilha.Cell("A" + linha.ToString()).Value.ToString();
                
                if (string.IsNullOrEmpty(Equipamento))
                {
                    break;
                }

            }
            */
            
            planilha.Cell("B3").Value = textBox2.Text;
            planilha.Cell("C2").Value = textBox3.Text;
            planilha.Cell("D2").Value = textBox4.Text;
            planilha.Cell("E2").Value = textBox5.Text;
            planilha.Cell("F2").Value = textBox6.Text;
            planilha.Cell("G2").Value = textBox7.Text;

            DataEmprestimo = CalEmp.SelectionRange.Start.ToShortDateString();
            planilha.Cell("H2").Value = DataEmprestimo;

            
            DataDevolucao = CalDev.SelectionRange.Start.ToShortDateString();
            planilha.Cell("I2").Value = DataDevolucao;



            MessageBox.Show("Equipamento cadastrado");
            workbook.SaveAs(@"c:\data\teste.xlsx");

            //Process.Start(new ProcessStartInfo(@"c:\data\teste.xlsx") { UseShellExecute = true });
            workbook.Dispose();
            
            
        }

        
    }

    private void groupBox1_Enter(object sender, EventArgs e)
    {
        
    }

    private void btnSair_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Process.Start(new ProcessStartInfo(@"c:\data\teste.xlsx") { UseShellExecute = true });
    }
}
  • I couldn’t understand, I tested your code and it’s working perfectly. I closed the application, opened it again and the data is still saved in the file... Could you elaborate better on what is happening? Closedxml library version also

  • Try to register more than one device, it is overwriting the existing file.

  • Ok, so you create the file once with some information, and when it runs again with different information, you want it to generate a second file? Because if you pass the same file, it will even overwrite

  • I want to use the.xlsx file as a database, I want to register and write to the spreadsheet, and consult later.

No answers

Browser other questions tagged

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