WPF textbox.text to excel file

pogipogi

New member
Joined
Jan 6, 2016
Messages
1
Programming Experience
1-3
I am new to WPF and have been spending hrs researching on how to copy the text that is in a textbox on WPF and put it into certain cells in an existing excel document. I have some code that I found online but this creates a new excel document. Any help would be great.
        private void Generatebtn_Click(object sender, RoutedEventArgs e)
        {
            //create new xls file
            string file = "Invoice.xls";


            
            Workbook workbook = new Workbook();
            Worksheet worksheet = new Worksheet("First Sheet");                         
            worksheet.Cells[10, 1] = new Cell(Addresstbx.Text);               //this is my addresstextbox.text
            //Workbook workbook = new Workbook();
            //Worksheet worksheet = new Worksheet("First Sheet");
            //worksheet.Cells[10, 1] = new Cell(Addresstbx.Text);
            //worksheet.Cells[2, 0] = new Cell(Businessnmntbx.Text);
            //worksheet.Cells[3, 3] = new Cell((decimal)3.45);
            //worksheet.Cells[2, 2] = new Cell("Text string");
            //worksheet.Cells[2, 4] = new Cell("Second string");
            //worksheet.Cells[4, 0] = new Cell(32764.5, "#,##0.00");
           // worksheet.Cells[5, 1] = new Cell(DateTime.Now, @"YYYY\-MM\-DD");
            worksheet.Cells.ColumnWidth[0, 1] = 3000;
            workbook.Worksheets.Add(worksheet);
            workbook.Save(file);


            // open xls file
            Workbook book = Workbook.Load(file);
            Worksheet sheet = book.Worksheets[0];


            // traverse cells
            //foreach (Pair<Pair<int, int>, Cell> cell in sheet.Cells)
            //{
            //    dgvCells[cell.Left.Right, cell.Left.Left].Value = cell.Right.Value;
            //}


            // traverse rows by Index
            for (int rowIndex = sheet.Cells.FirstRowIndex;
                   rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
            {
                Row row = sheet.Cells.GetRow(rowIndex);
                for (int colIndex = row.FirstColIndex;
                   colIndex <= row.LastColIndex; colIndex++)
                {
                    Cell cell = row.GetCell(colIndex);
                }
            }
            MessageBox.Show("File Created");
        }
 
Last edited by a moderator:
This really has nothing to do with WPF. You get a String from your WPF TextBox from its Text property. That's the sum total of the WPF involvement. You now have a String like any other String and you can use it like any other String. Your issue is getting a String into an Excel cell, which has nothing to do with WPF. The solution will be the same regardless of where the String comes from.

If you have code that can put text into a cell then you're most of the way there. All you need to do is find out how to open an existing Excel file in C#. I don't know how to do that but I'll wager it would take me a few minutes to find out with a web search.
 

Latest posts

Back
Top Bottom