c#格式化数据保存到excel文件

时间:2013-04-23 11:56:03

标签: c# excel kinect

我正在使用Kinect并尝试将跟踪坐标保存到excel文件中。我已设法创建excel文件并保存数据,但我需要格式化数据的保存方式以创建图形。我该怎么做?

数据当前显示在一个单元格中:

23-04-13 10:24

头:X:411 Y:255

肩部中心:X:417 Y:317

ShoulderLeft:X:360 Y:353

ShoulderRight:X:474 Y:357

ElbowLeft:X:341 Y:412

ElbowRight:X:513 Y:426

WristLeft:X:321 Y:451

WristRight:X:513 Y:471

HandLeft:X:312 Y:454

HandRight:X:502 Y:471

脊椎:X:422 Y:389

HipCenter:X:422 Y:403

HipLeft:X:397 Y:429

HipRight:X:450 Y:434

KneeLeft:X:305 Y:484

KneeRight:X:316 Y:425

AnkleLeft:X:205 Y:547

AnkleRight:X:426 Y:437

FootLeft:X:178 Y:559

FootRight:X:394 Y:441

我需要将坐标值放在不同的单元格中。

我保存为excel的代码。

}

        TextBox1.Text = Head;
        TextBox2.Text = ShoulderCenter;
        TextBox3.Text = ShoulderLeft;
        TextBox4.Text = ShoulderRight;
        TextBox5.Text = ElbowLeft;
        TextBox6.Text = ElbowRight;
        TextBox7.Text = WristLeft;
        TextBox8.Text = WristRight;
        TextBox9.Text = HandLeft;
        TextBox10.Text = HandRight;
        TextBox11.Text = Spine;
        TextBox12.Text = HipCenter;
        TextBox13.Text = HipLeft;
        TextBox14.Text = HipRight;
        TextBox15.Text = KneeLeft;
        TextBox16.Text = KneeRight;
        TextBox17.Text = AnkleLeft;
        TextBox18.Text = AnkleRight;
        TextBox19.Text = FootLeft;
        TextBox20.Text = FootRight;


        {
            StreamWriter sw = File.AppendText("Data.xls");
            sw.WriteLine(DateTime.Now);
            sw.WriteLine(TextBox1.Text);
            sw.WriteLine(TextBox2.Text);
            sw.WriteLine(TextBox3.Text);
            sw.WriteLine(TextBox4.Text);
            sw.WriteLine(TextBox5.Text);
            sw.WriteLine(TextBox6.Text);
            sw.WriteLine(TextBox7.Text);
            sw.WriteLine(TextBox8.Text);
            sw.WriteLine(TextBox9.Text);
            sw.WriteLine(TextBox10.Text);
            sw.WriteLine(TextBox11.Text);
            sw.WriteLine(TextBox12.Text);
            sw.WriteLine(TextBox13.Text);
            sw.WriteLine(TextBox14.Text);
            sw.WriteLine(TextBox15.Text);
            sw.WriteLine(TextBox16.Text);
            sw.WriteLine(TextBox17.Text);
            sw.WriteLine(TextBox18.Text);
            sw.WriteLine(TextBox19.Text);
            sw.WriteLine(TextBox20.Text);

            sw.Close();          

    }

1 个答案:

答案 0 :(得分:0)

首先,将你的值放在一个数组(IEnumerable,List或其他)中,这样你就可以通过循环来完成它。

using Excel = Microsoft.Office.Interop.Excel;

使用:

public void doExcel()
{

    Excel.Application myExcelApplication = new Excel.Application();
    myExcelApplication.Visible = true; 
    myExcelApplication.ScreenUpdating = true;
    Excel.Workbook myExcelWorkbook = (Excel.Workbook) myExcelApplication.Workbooks.Add(System.Reflection.Missing.Value)); 
    Excel.Worksheet myExcelWorkSheet = (Excel.Worksheet)myExcelWorkbook.ActiveSheet;

    for (int i = 0; i < myList.Count; i++)
    {
        myExcelWorkSheet.Cells[3, i] = String.Format("{0}", myList[i]);                
    }
    myExcelWorkbook.Close(true, "C:\\data.xls", System.Reflection.Missing.Value);
}