菜鸟求助啊,最近刚接触C#,求高手帮忙写一段代码,将三个存放在textbox里的数据(分别叫做σ1,σ2,σ3)导出到excel表格里,

解决方案 »

  1.   


                Excel.Application xlApp = new Excel.Application();
                Workbook xlBook = xlApp.Workbooks.Add(true);
                Worksheet wsMask = (Worksheet)xlBook.Worksheets["sheet1"];
                wsMask.Columns.ColumnWidth = 2;
                wsMask.Columns.RowHeight = 13.5;
                wsMask.Name = ""; //sheet的名称 
                Range range = wsMask.get_Range("A2", "A2");
                range.Value = textBox1.Text;
    保存成07的  xlBook.SaveAs(outputFileName, 56, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);保存成03的  
     xlBook.SaveAs(outputFileName, 43, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
      

  2.   

    还是没试出来.......我现在的做winform里有三个存放计算结果的textbox,一个导出botton,我想通过单击导出botton来将三个存放在textbox里的结果导出到excel,请问该写代码
      

  3.   

    http://www.cnblogs.com/peterzb/archive/2009/07/06/1517399.html
      

  4.   

    首先要添加引用:using Microsoft.Office.Interop;Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                excel.Application.Workbooks.Add(true);
                excel.Visible = true;
    ///然后将你要添加的值添加的excel表格中
    //下面这个是参考
    //   excel.Cells[i,j] =TextBox1.text;
    //   excel.FindFormat.Font.Bold = true;
      

  5.   

    给你个参考,如下
    使用前先添加EXCEL引用        /// <summary>
            /// 打开EXCEL 把DataGridView中的数据写入EXCEL  完成后显示excel 
            /// </summary>
            /// <param name="dg"></param>
            public static void input_excel2(DataGridView dg)
            {
                Excel._Worksheet Sht;
                Excel._Workbook Bo;
                Excel.Application excel = new Excel.Application();
                Bo = excel.Application.Workbooks.Add(true);
                // excel.Visible = true;
                Sht = (Excel.Worksheet)Bo.Sheets[1];
                //写入数据到EXCEL
                int Rowed = 0;           
                if (dg.AllowUserToAddRows == true)
                {              
                    for (int i = 0; i < dg.Rows.Count; i++)
                    {
                        for (int y = 1; y <= dg.ColumnCount; y++)
                        {
                            excel.Cells[1, y] = dg.Columns[y - 1].HeaderText;
                        }
                        Rowed++;
                        if (Rowed < 65000)
                        {
                            for (int lie = 0; lie < dg.ColumnCount; lie++)
                            {
                                excel.Cells[Rowed + 1, lie + 1] = Convert.ToString(dg[lie, i].Value);
                            }
                        }
                        else
                        {
                            Sht = (Excel.Worksheet)Bo.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                            Rowed = 0;
                            i--;
                        }
                    }
                }
                else
                {               
                    for (int i = 0; i < dg.Rows.Count; i++)
                    {
                        for (int y = 1; y <= dg.ColumnCount; y++)
                        {
                            excel.Cells[1, y] = dg.Columns[y - 1].HeaderText;
                        }
                        Rowed++;
                        if (Rowed < 65000)
                        {
                            for (int lie = 0; lie < dg.ColumnCount; lie++)
                            {
                                excel.Cells[Rowed + 1, lie + 1] = Convert.ToString(dg[lie, i].Value);
                            }
                        }
                        else
                        {
                            Sht = (Excel.Worksheet)Bo.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                            Rowed = 0;
                            i--;
                        }
                    }
                }          
                excel.Visible = true;
            }