码迷,mamicode.com
首页 > 其他好文 > 详细

C# 创建Excel并写入内容

时间:2014-09-24 12:08:36      阅读:423      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   sp   cti   on   c   ad   line   

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();
        }

 

C# 创建Excel并写入内容

标签:io   os   ar   sp   cti   on   c   ad   line   

原文地址:http://www.cnblogs.com/victorgui/p/3989776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!