1

Excel Interopを使用してExcelファイルを作成し、それにデータを出力しています。コンボボックスからのユーザーの選択に基づいてワークシート名を変更したいと思います。ただし、comboBoxから入力を取得して、ワークシート名として表示することができません。ただし、ワークシート名がテキストボックスから送信される場合は、ワークシート名と同じ値を表示できます。私はそれをcomboBox.SelectedItem.ToString()文字列として使用して作成し、それをワークシート名として適用しようとしています。英字以外の文字をスペースに置き換えても機能しませんでした。刺し傷には英字とスペースしかありませんが、元のワークシート名は置き換えられません。

ワークシート名を変更するために使用しているコードは次のとおりです。

worksheet = (Excel.Worksheet)workbook.Worksheets.Add(Missing.Value, workbook.Worksheets[sheetCount], Missing.Value, Missing.Value);
workbook.Worksheets[sheetCountPlusONe].Name = "Results " + registrationForm.selectedEvent;
4

1 に答える 1

1

これを試して。registrationFormそれがコンボボックスだと思います。

GetItemText選択したアイテムを分析し、最終的にそのアイテムのテキストを返すメソッドを使用する必要があります。

string WsName = this.registrationForm.GetItemText(this.registrationForm.SelectedItem);

試してテストしました

    private void Form1_Load(object sender, EventArgs e)
    {
        this.comboBox1.Items.Add("Sheet1111");
        this.comboBox1.Items.Add("Sheet2222");
        this.comboBox1.Items.Add("Sheet3333");
    }

   private void btnOPen_Click(object sender, EventArgs e)
    {
        Microsoft.Office.Interop.Excel.Application xlexcel;
        Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;

        object misValue = System.Reflection.Missing.Value;
        xlexcel = new Excel.Application();

        xlexcel.Visible = true;

        //~~> Open a File
        xlWorkBook = xlexcel.Workbooks.Open("C:\\sample.xlsx", 0, true, 5, "", "", true,
        Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

        // Set Sheet 1 as the sheet you want to work with
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        string WsName = this.comboBox1.GetItemText(this.comboBox1.SelectedItem);

        xlWorkSheet.Name = WsName;

       //
       //~~> Rest of code
       //
    }
于 2012-08-10T02:42:31.337 に答える