私はこの機能を使用して、Excelファイルに書き込み、保存します。私が今やりたいことは、同じ既存のエクセルfile.Thisに追加され関数である。それが存在し、それが存在する場合、追加操作を行う場合既存のExcelファイルに追加する
private void button8_Click(object sender, EventArgs e)//creates excel file and writes data to it
{
Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("Excel is not properly installed!!");
return;
}
if (System.IO.File.Exists("cross_check.xls"))
{
}
else
{
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
IndexProp += 1;
xlWorkSheet.Cells[IndexProp, 1] = comboBox2.Text;
xlWorkSheet.Cells[IndexProp, 2] = textBox5.Text;
xlWorkSheet.Cells[IndexProp, 3] = textBox2.Text;
xlWorkSheet.Cells[IndexProp, 4] = comboBox3.Text;
xlWorkSheet.Cells[IndexProp, 5] = textBox3.Text;
xlWorkSheet.Cells[IndexProp, 6] = comboBox1.Text;
xlWorkBook.SaveAs(@"cross_check.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
MessageBox.Show("Excel file created succcessfully");
}
}
}
この特定のブロックはファイルを検査しています。これはどうすればいいですか?
if (System.IO.File.Exists("cross_check.xls"))
{
}
Excel機能を使用していない限り、Excelが簡単に操作できる.CSVファイルを作成して追加するほうがはるかに簡単です。私はInteropが困難であることを発見しました。特にプログラムが異なるPC上で動作する場合は特にそうです。 Excelの機能が必要な場合は、次にExcelでVBAを使用して作業してみてください。 Interopが私の最後の選択です。 – rheitzman
@rheitzman問題は、このExcelファイルをデータグリッドに読み込んだり、しばらくして.CSVファイルに問題が生じる可能性があることです。 – Jevon