Hello everyone,
I know how to take a DataGridView and put it into Excel, but how can I take the text that's in a textbox and put it into a specific cell in Excel?
Here's my code for DataGridViewToXLS:
However, I would like to have TextBoxTextToXLS. Thanks.Code://this whole section will transfer everything to Excel, from here to the end. Excel.Application excelApp = new Excel.Application(); Excel._Workbook excelWB; Excel._Worksheet excelWS; Excel.Range excelRng; //Opens a new instance excelWB = (Excel._Workbook)(excelApp.Workbooks.Add(Missing.Value)); excelWS = (Excel._Worksheet)excelWB.ActiveSheet; int colIndex = 0; foreach (DataGridViewColumn column in dataGridView1.Columns) { if (column.Visible) { //Export excelWS.Cells[1, colIndex + 1] = column.HeaderText; for (int row = 1; row < dataGridView1.Rows.Count; row++) { excelWS.Cells[row + 1, colIndex + 1] = dataGridView1[colIndex, row - 1].Value; } colIndex++; } }
I hate to say this to you...but none of that makes sense to me. I'm a beginner, remember?
That's what I have right now. I know how to create an excel app, but I'm having trouble with the textbox it appears. It was kind of working, but now how I want it. Now i'm getting an error about instances.Code:Excel.Application xlApp = new Excel.Application(); Excel.Workbook xlWB; Excel.Worksheet xlWS; Excel.Range xlRng; string workbookPath = "C:\\Documents and Settings\\adavis\\My Documents\\Area51\\T-Numbers Data"; private void addTNumberBtn_Click(object sender, EventArgs e) { xlApp.Workbooks.Open(workbookPath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); int colIndex = 0; string addTNumbers = addTNumberTextBox.Text; xlWS.Cells[1, 1] = addTNumbers; //Save the file to where you specified xlWB.Save(); xlApp.Visible = true; }
It builds and compiles just fine, but when I run it, and click my button, it says:
Object reference not set to an instance of an object.
string addTNumbers = addTNumberTextBox.Text;
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks