• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#创建Excel并写入内容

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1 增加应用      Microsoft.Office.Interop.Excel 
        2 引用命名空间  using Excel = Microsoft.Office.Interop.Excel; 
        /// <summary>  
        /// If the supplied excel File does not exist then Create it  
        /// </summary>  
        /// <param name="FileName"></param>  
        private void CreateExcelFile(string FileName) 
        { 
            //create  
            object Nothing = System.Reflection.Missing.Value; 
            var app = new Excel.Application(); 
            app.Visible = false; 
            Excel.Workbook workBook = app.Workbooks.Add(Nothing); 
            Excel.Worksheet worksheet = (Excel.Worksheet)workBook.Sheets[1]; 
            worksheet.Name = "Work"; 
            //headline  
            worksheet.Cells[1, 1] = "FileName"; 
            worksheet.Cells[1, 2] = "FindString"; 
            worksheet.Cells[1, 3] = "ReplaceString"; 
 
            worksheet.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing); 
            workBook.Close(false, Type.Missing, Type.Missing); 
            app.Quit(); 
        } 
 
        /// <summary>  
        /// open an excel file,then write the content to file  
        /// </summary>  
        /// <param name="FileName">file name</param>  
        /// <param name="findString">first cloumn</param>  
        /// <param name="replaceString">second cloumn</param>  
        private void WriteToExcel(string excelName,string filename,string findString,string replaceString) 
        { 
            //open  
            object Nothing = System.Reflection.Missing.Value; 
            var app = new Excel.Application(); 
            app.Visible = false; 
            Excel.Workbook mybook = app.Workbooks.Open(excelName, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing); 
            Excel.Worksheet mysheet = (Excel.Worksheet)mybook.Worksheets[1]; 
            mysheet.Activate();      
            //get activate sheet max row count  
            int maxrow = mysheet.UsedRange.Rows.Count + 1; 
            mysheet.Cells[maxrow, 1] = filename; 
            mysheet.Cells[maxrow, 2] = findString; 
            mysheet.Cells[maxrow, 3] = replaceString; 
            mybook.Save(); 
            mybook.Close(false, Type.Missing, Type.Missing); 
            mybook = null; 
            //quit excel app  
            app.Quit(); 
        } 

        1 增加应用      Microsoft.Office.Interop.Excel
        2 引用命名空间  using Excel = Microsoft.Office.Interop.Excel;
        /// <summary>
        /// If the supplied excel File does not exist then Create it
        /// </summary>
        /// <param name="FileName"></param>
        private void CreateExcelFile(string FileName)
        {
            //create
            object Nothing = System.Reflection.Missing.Value;
            var app = new Excel.Application();
            app.Visible = false;
            Excel.Workbook workBook = app.Workbooks.Add(Nothing);
            Excel.Worksheet worksheet = (Excel.Worksheet)workBook.Sheets[1];
            worksheet.Name = "Work";
            //headline
            worksheet.Cells[1, 1] = "FileName";
            worksheet.Cells[1, 2] = "FindString";
            worksheet.Cells[1, 3] = "ReplaceString";

            worksheet.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
            workBook.Close(false, Type.Missing, Type.Missing);
            app.Quit();
        }

        /// <summary>
        /// open an excel file,then write the content to file
        /// </summary>
        /// <param name="FileName">file name</param>
        /// <param name="findString">first cloumn</param>
        /// <param name="replaceString">second cloumn</param>
        private void WriteToExcel(string excelName,string filename,string findString,string replaceString)
        {
            //open
            object Nothing = System.Reflection.Missing.Value;
            var app = new Excel.Application();
            app.Visible = false;
            Excel.Workbook mybook = app.Workbooks.Open(excelName, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
            Excel.Worksheet mysheet = (Excel.Worksheet)mybook.Worksheets[1];
            mysheet.Activate();    
            //get activate sheet max row count
            int maxrow = mysheet.UsedRange.Rows.Count + 1;
            mysheet.Cells[maxrow, 1] = filename;
            mysheet.Cells[maxrow, 2] = findString;
            mysheet.Cells[maxrow, 3] = replaceString;
            mybook.Save();
            mybook.Close(false, Type.Missing, Type.Missing);
            mybook = null;
            //quit excel app
            app.Quit();
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
【转】解决:fatalerrorC1083:无法打开预编译头文件发布时间:2022-07-14
下一篇:
C++里的常用头文件发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap