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: