Import C# array into Excel

private void button1_Click(object sender, EventArgs e)
{
  try
  {
    //1.创建Applicaton对象
    Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.Application();

    //2.得到workbook对象,打开已有的文件
    Microsoft.Office.Interop.Excel.Workbook xBook = xApp.Workbooks.Open(textBox3.Text + textBox4.Text + ".xlsx",
    Missing.Value, Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value, Missing.Value);

    //3.指定要操作的Sheet
    Microsoft.Office.Interop.Excel.Worksheet xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[1];

   
    for (int i = 0; i < array.Count; i++)
    {
      //After the array is read in a loop, the data is stored in the first column of Excel

          //Cells[column][line]
      xSheet.Cells[1][i + 1] = list2[i];
    }


    //5. Save and save
    the WorkBook xBook.Save();
    //6. Close the Excel object from memory
    xSheet = null;
    xBook.Close();
    xBook = null;
    //Close the Excel prompt box
    xApp.DisplayAlerts = false;
    //Excel exits from memory
    xApp.Quit();
    xApp = null;
    MessageBox.Show("File export successfully!");
  }
  catch(IOException eee)
  {
    MessageBox.Show("File export failed!");
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325253909&siteId=291194637