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

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人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#正则表达式的用法发布时间:2022-07-18
下一篇:
c#RedisHelper发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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