トップ回答者
Visual C# Excelへの書き込みについて

質問
-
Visual c# エクセル操作について質問です。 今クイズのアプリケーションを作成していて、全部で50問あり、PictureBoxに問題の画像を表示させ、button1を押すと次の問題へ、button2を押すと前の問題へいくようにしています。そこで1~4の値を持ったRadiobuttonを使って、ボタンを押すとエクセルのセル( B2~B51 )にそれぞれ書き込みたいのですが、やり方がわからないので、教えていただきたいです。プログラム
using System.ComponentModel.Component;
using System.Windows.Forms.Control;
using System.Windows.Forms.ButtonBase;
using System.Windows.Forms.RadioButton;
using Microsoft.Office.Tools.Excel.Controls.RadioButton;private void Form1_Load(object sender, EventArgs e)
{
Excel.Application oXls; // Excelオブジェクト
oXls = new Excel.Application(); //シートは1つのみ
oXls.Visible = true;
oWBook = (Excel.Workbook)(oXls.Workbooks.Open(
@"F:\test.xlsx" // オープンするExcelファイル名
));}private const int Qnumber = 50;//回答番号のカウント
private int Qnumber = 0;//最初の問題private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
selected[num] = 1;
}private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
selected[num] = 2;
}private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
selected[num] = 3;
}private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
selected[num] = 4;
}ここのつづきにエクセルに書き込めるようにするコードを書くといいと思うのですが、どんな風になるのかよくわからないです。
Visual C# 初心者で、いろいろ間違っているところがあるかもしれませんが、詳しく教えていただきたいです。
回答よろしくお願いします。
回答
-
こんな
using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using Excel = Microsoft.Office.Interop.Excel; using System.Runtime.InteropServices; namespace WindowsFormsApplication1 { public partial class Form1 : Form { List<int> selected = new List<int>();//回答結果 int num;//問題番号 int MAXNUM = 49; Label lblNum; public Form1() { InitializeComponent(); num = 0; for (int i = 0; i <= MAXNUM; i++) { selected.Add(0); } int y = 0; lblNum = new Label(); lblNum.Text = num.ToString(); this.Controls.Add(lblNum); y += lblNum.Height + 2; for (int i = 1; i <= 4; i++) { var r = new RadioButton(); r.Text = i.ToString(); r.Tag = i; r.Top = y; r.CheckedChanged += radio_CheckedChanged; this.Controls.Add(r); y += r.Height + 2; } foreach (Button btn in new Button[] { new Button() { Text = "次" ,Tag=1} , new Button() {Text="前", Tag=-1} }) { btn.Click += btn_Click; btn.Top = y; this.Controls.Add(btn); y += btn.Height + 2; } Button saveButton = new Button(); saveButton.Text = "SAVE"; saveButton.Click += saveButton_Click; saveButton.Top = y; this.Controls.Add(saveButton); } void btn_Click(object sender, EventArgs e) { int step = (int)((Button)sender).Tag; num = System.Math.Max(0, System.Math.Min(num + step, MAXNUM)); lblNum.Text = num.ToString(); foreach (RadioButton r in this.Controls.OfType<RadioButton>()) { r.Checked = (int)r.Tag == selected[num]; } } void radio_CheckedChanged(object sender, EventArgs e) { var r = (RadioButton)sender; if (r.Checked) { selected[num] = (int)r.Tag; } } void saveButton_Click(object sender, EventArgs e) { List<object> comobjects = new List<object>(); Excel.Application xla; // Excelオブジェクト Excel.Workbooks xlBooks; Excel.Workbook xlBook; Excel.Sheets xlSheets; Excel.Worksheet xlSheet; Excel.Range rB2; xla = new Excel.Application(); //シートは1つのみ try { comobjects.Add(xla); xla.Visible = true; xlBooks = xla.Workbooks; try { xlBook = xlBooks.Open(@"D:\Book1.xlsx"); // オープンするExcelファイル名 try { xlSheets = xlBook.Worksheets; try { xlSheet = xlSheets[1]; try { rB2 = xlSheet.Range["B2"]; foreach (int value in selected) { rB2.Value = value; Excel.Range rTemp; rTemp = rB2.Offset[1, 0]; Marshal.ReleaseComObject(rB2); rB2 = rTemp; } Marshal.ReleaseComObject(rB2); } finally { Marshal.ReleaseComObject(xlSheet); } } finally { Marshal.ReleaseComObject(xlSheets); } xlBook.Save(); xlBook.Close(); } catch { Marshal.ReleaseComObject(xlBook); } } finally { Marshal.ReleaseComObject(xlBooks); } xla.Quit(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { Marshal.ReleaseComObject(xla); } } } }
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
- 回答の候補に設定 栗下 望Microsoft employee, Moderator 2017年1月18日 7:20
- 回答としてマーク 栗下 望Microsoft employee, Moderator 2017年1月23日 9:48