您的位置:首页 > 编程语言 > C#

把数据填到EXcel里

2016-03-04 14:46 405 查看
             string currentPath = string.Empty;//= System.Environment.CurrentDirectory;

            string filename = Convert.ToString(DateTime.Now.ToFileTimeUtc()) + ".xlsx";

            string path = string.Empty;

            SaveFileDialog filedialg = new SaveFileDialog();

            filedialg.FileName = filename; // Default file name

            filedialg.DefaultExt = ".xlsx"; // Default file extension

            filedialg.Filter = "Excel documents (.xlsx)|*.xlsx"; // Filter files by extension

            // Show open file dialog box

            Nullable<bool> result = filedialg.ShowDialog();

            if (result == true)

            {

                path = filedialg.FileName;

            }


  object misValue = System.Reflection.Missing.Value;

                Microsoft.Office.Interop.Excel.Application xl = new Microsoft.Office.Interop.Excel.Application();

                Workbook xlworkbook = xl.Workbooks.Add(misValue);

                Worksheet xlworksheet = xlworkbook.Worksheets.get_Item(1);

                xlworksheet.Cells.RowHeight = 20;

                xlworksheet.Cells.ColumnWidth = 30;

                for (int i = 0; i < messagelist.Count; i++)

                {

                    string s = messagelist[i].Substring(messagelist[i].Length - 2);

                    if (Convert.ToInt32(s, 16) % 2 == 0)

                    {

                        xlworksheet.Cells[i / 2 + 1, 1] = messagelist[i];

                    }

                    else

                    {

                        xlworksheet.Cells[i / 2 + 1, 2] = messagelist[i];

                    }

                }

                xl.DisplayAlerts = false;

                xlworkbook.SaveAs(path, XlFileFormat.xlWorkbookDefault, Missing.Value, Missing.Value, Missing.Value, Missing.Value,

                    Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value,

                    Missing.Value, Missing.Value);

                xlworkbook.Close(true, misValue, misValue);

                xl.Quit();            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# 实例 函数