Skip to content

Instantly share code, notes, and snippets.

@saitohiroaki1122
Created April 9, 2018 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saitohiroaki1122/eb3849cc67855889f24ef378b66fe243 to your computer and use it in GitHub Desktop.
Save saitohiroaki1122/eb3849cc67855889f24ef378b66fe243 to your computer and use it in GitHub Desktop.
GH C# Component tips_GrasshopperからExcelに書き込む
private void RunScript(bool setParam, string filePath)
{
if(setParam)
{
Microsoft.Office.Interop.Excel.Application exlApp =
new Microsoft.Office.Interop.Excel.Application();
string excelBookFilePath = filePath;
exlApp.Visible = true;
Microsoft.Office.Interop.Excel.Workbook wb = exlApp.Workbooks.Add();
Microsoft.Office.Interop.Excel.Worksheet ws1 = (Worksheet) wb.Sheets[1];
//Type.Missingの説明について
//http://d.hatena.ne.jp/ron_s-nakazawa/20120924/1348452525#20120924f1
ws1.Select(Type.Missing); //Sheetを選択する。今回はなくても恐らく問題はない
Range range22 = (Range) ws1.Cells[2, 2]; //セルを指定する
range22.Value = "Eye Level(mm)"; //セルにパラメーターを入力
range22.BorderAround(XlLineStyle.xlContinuous); //セルの四辺に罫線を描く
range22.Cells.Interior.Color = Color.LightGray; //セルに色を付ける
range22.Columns.ColumnWidth = 25; //セルの幅を調整する
Range range32 = (Range) ws1.Cells[3, 2];
range32.Value = "C-Value(mm)";
range32.BorderAround(XlLineStyle.xlContinuous);
range32.Cells.Interior.Color = Color.LightGray;
Range range23 = (Range) ws1.Cells[2, 3];
range23.Value = 1200;
range23.BorderAround(XlLineStyle.xlContinuous);
//バリデーションで0より小さい数字は入力チェックではじく。ただのチェックなので書かなくても入力自体は問題なくできる。
range23.Validation.Add(XlDVType.xlValidateDecimal, XlDVAlertStyle.xlValidAlertStop,
XlFormatConditionOperator.xlGreater, 0);
Range range33 = (Range) ws1.Cells[3, 3];
range33.Value = 120;
range33.BorderAround(XlLineStyle.xlContinuous);
range33.Validation.Add(XlDVType.xlValidateDecimal, XlDVAlertStyle.xlValidAlertStop,
XlFormatConditionOperator.xlGreater, 0);
wb.SaveAs(excelBookFilePath);
//上記までで開いた状態で処理を終了する。
//下記は書き込んだ後に自動的にExcelファイルを閉じる。
//wb.Close(false);
//exlApp.Quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment