【问题标题】:How export textbox data into excel file?如何将文本框数据导出到excel文件中?
【发布时间】:2015-10-01 14:25:02
【问题描述】:

我有 2 个文本框,即 textbox1,textbox2

我想将这些文本框中的数据导出到 Excel 工作表,即 test.xlsx 使用按钮。

有人知道这样做的代码是什么吗?

【问题讨论】:

  • 哈哈哈......“任何人都知道这样做的代码是什么”。大声笑
  • @SandeepKushwah 我愿意 =D
  • 你搜索过吗?看:How to write some data to excel file
  • 我试过这段代码 > System.IO.File.WriteAllText("test.xlsx", textBox1.Text)
  • @Abdullah :如果它解决了您的问题,请将答案标记为已接受的解决方案,否则发布您自己的答案以解决您的问题。

标签: c# windows export


【解决方案1】:

首先,您需要在项目中添加对 Excel 对象库的引用。

您可以将库导入表单:

using Excel = Microsoft.Office.Interop.Excel;

你可以添加类似的代码:

        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;

您也可以查看this Walkthrough了解详细说明

【讨论】:

    【解决方案2】:
    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();
    

    【讨论】:

      猜你喜欢
      • 2020-09-05
      • 1970-01-01
      • 2012-12-19
      • 2013-06-12
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 2013-06-22
      相关资源
      最近更新 更多