2017/10/15エディタテスト
2123 ワード
2017/10/15:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
namespace Excel2
{
public partial class Form1 : Form
{
string cruentPath = "";
public Form1()
{
InitializeComponent();
init();
}
private void button1_Click(object sender, EventArgs e)
{
WriteExcel(cruentPath + "\\results.xlsx");
}
private void init()
{
cruentPath = Directory.GetCurrentDirectory();
}
private void WriteExcel(string filename)
{
Excel.Application excelApp = new Excel.Application();
if (excelApp == null)
{
MessageBox.Show(" Excel");
return;
}
Excel.Workbook workbook;
if (File.Exists(filename))
{
workbook = excelApp.Workbooks.Open(filename, 0, false, 5, "", "", true,
Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
}
else
{
workbook = excelApp.Workbooks.Add(true);
}
Excel.Worksheet worksheet = workbook.ActiveSheet as Excel.Worksheet;
worksheet = (Excel.Worksheet)workbook.Worksheets.get_Item(1);
worksheet.Cells[1,1] = " ";
worksheet.Cells[1, 2] = " 1";
worksheet.Cells[1, 3] = " 2";
worksheet.Cells[1, 4] = " 3";
//worksheet.Cells[1, 2] = " ";
excelApp.Visible = false;
excelApp.DisplayAlerts = false;
if (File.Exists(filename))
{
workbook.Save();
}
//workbook.SaveAs(filename);
workbook.Close(false,Missing.Value,Missing.Value);
excelApp.Quit();
worksheet = null;
workbook = null;
excelApp = null;
GC.Collect();
}
}
}