3

I have 2 textbox i.e. textbox1,textbox2

I want to export the data from these textboxes to an excel sheet i.e. test.xlsx using a button.

Anyone know what's the code to do that?

Adam Strauss
  • 1,889
  • 2
  • 15
  • 45

2 Answers2

3

First, you need to add a reference to the Excel Object Library in your project.

You can than import the library to your form:

using Excel = Microsoft.Office.Interop.Excel;

You can add similar code to this:

        var excelApp = new Excel.Application();       

        excelApp.Workbooks.Open(filePath);            
        Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;      
        workSheet.Cells[1, "A"] = textBox1.Text;
        workSheet.Cells[1, "B"] = textBox1.Text;

You can also check this Walkthrough for an detailed explanation

S.Dav
  • 2,436
  • 16
  • 22
0
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;

string myPath = tbFolderpath.Text + tbFileName.Text;//User Given Path Value
FileInfo fi = new FileInfo(myPath);
Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (!fi.Exists)//To Check File exist in a location,if not exist it will create new file
{
 xlWorkBook = xlApp.Workbooks.Add(misValue);
 xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
 xlWorkSheet.Cells[1, "A"] = "Name";
 xlWorkSheet.Cells[1, "B"] = "Age";
 xlWorkSheet.Cells[1, "C"] = "CurrentTime";
 var columnHeadingsRange = xlWorkSheet.Range[xlWorkSheet.Cells[1, "A"], 
                           xlWorkSheet.Cells[1, "C"]];
 columnHeadingsRange.Interior.Color = Excel.XlRgbColor.rgbYellow;//To Give Header Color
 xlWorkBook.SaveAs(myPath, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, 
                   misValue,misValue, misValue, misValue, 
                   Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, 
                   misValue, misValue, misValue, misValue, misValue);
}
//Already File Exist it will open the File and update the data into excel`enter code here`
var workbook = xlApp.Workbooks.Open(myPath);
xlWorkSheet = (Excel.Worksheet)workbook.Worksheets.get_Item(1);
int _lastRow = xlWorkSheet.Range["A" +xlWorkSheet.Rows.Count]. 
               End[Excel.XlDirection.xlUp].Row + 1;
xlWorkSheet.Cells[_lastRow, "A"] = Textbox1.Text;
xlWorkSheet.Cells[_lastRow, "B"] = Textbox2.Text;
DateTime currentTime = DateTime.Now;//To Get the Current Time
string formattedTime = currentTime.ToString("dd/MM/yyyy-hh:mm:ss");
xlWorkSheet.Cells[_lastRow, "C"] = formattedTime;
workbook.Save();
workbook.Close();
xlApp.Quit();
KRV
  • 1
  • 1