C# TO Excel

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;

using  Excel  =  Microsoft.Office.Interop.Excel;
using  System.Reflection;

namespace  WinFormTest
{
    
public   class  ExcelProvider
    {
        
// private Excel.Application objApp;
        
// private Excel._Workbook objBook;

        
public  ExcelProvider()
        { }

        
public   void  Create()
        {

            
// 创建Application对象 
            Excel.Application xApp  =   new  Excel.ApplicationClass();

            xApp.Visible 
=   false ;
            
// 得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件 
            Excel.Workbook xBook  =  xApp.Workbooks._Open( @" C:\SampleTemplate.xls " ,
            Missing.Value, Missing.Value, Missing.Value, Missing.Value
            , Missing.Value, Missing.Value, Missing.Value, Missing.Value
            , Missing.Value, Missing.Value, Missing.Value, Missing.Value);
            
// xBook=xApp.Workbooks.Add(Missing.Value); // 新建文件的代码 
            
            
// 指定要操作的Sheet,两种方式:
            Excel.Worksheet xSheet  =  (Excel.Worksheet)xBook.Sheets.Sheets[ 1 ];
            
// Excel.Worksheet xSheet=(Excel.Worksheet)xApp.ActiveSheet; 

            
// 读取数据,通过Range对象 
            
// Excel.Range rng1 = xSheet.get_Range("A1", Type.Missing);
            
// Console.WriteLine(rng1.Value2);

            
// 读取,通过Range对象,但使用不同的接口得到Range 
            
// Excel.Range rng2 = (Excel.Range)xSheet.Cells[3, 1];
            
// Console.WriteLine(rng2.Value2);

            
// 写入数据 
            
            Excel.Range rng3 
=  xSheet.get_Range( " C6 " , Missing.Value);
            rng3.Value2 
=   " Hello " ;
            rng3.Interior.ColorIndex 
=   6 // 设置Range的背景色 

            
// 保存方式一:保存WorkBook 
            xBook.SaveAs( @" C:\SampleTemplate200809002.xls " ,
            Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
            Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value,
            Missing.Value, Missing.Value);

            
// 保存方式二:保存WorkSheet 
            
// xSheet.SaveAs(@"C:\SampleTemplate200809002.xls",
            
// Missing.Value, Missing.Value, Missing.Value, Missing.Value,
            
// Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);


            
// 保存方式三 
            
// xBook.Save();

            xSheet 
=   null ;
            xBook 
=   null ;
            xApp.Quit(); 
// 这一句是非常重要的,否则Excel对象不能从内存中退出 
            xApp  =   null


        }
    }
}

转载于:https://www.cnblogs.com/syringa-flz/archive/2008/09/02/1282231.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/// <summary> /// 导出到Excel /// </summary> /// <param name="table"></param> /// <returns></returns> public bool ToExcel(DataTable table) { FileStream fs = new FileStream(this._filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); IWorkbook workBook = new HSSFWorkbook(); this._sheetName = this._sheetName.IsEmpty() ? "sheet1" : this._sheetName; ISheet sheet = workBook.CreateSheet(this._sheetName); //处理表格标题 IRow row = sheet.CreateRow(0); row.CreateCell(0).SetCellValue(this._title); sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, table.Columns.Count - 1)); row.Height = 500; ICellStyle cellStyle = workBook.CreateCellStyle(); IFont font = workBook.CreateFont(); font.FontName = "微软雅黑"; font.FontHeightInPoints = 17; cellStyle.SetFont(font); cellStyle.VerticalAlignment = VerticalAlignment.Center; cellStyle.Alignment = HorizontalAlignment.Center; row.Cells[0].CellStyle = cellStyle; //处理表格列头 row = sheet.CreateRow(1); for (int i = 0; i < table.Columns.Count; i++) { row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName); row.Height = 350; sheet.AutoSizeColumn(i); } //处理数据内容 for (int i = 0; i < table.Rows.Count; i++) { row = sheet.CreateRow(2 + i); row.Height = 250; for (int j = 0; j < table.Columns.Count; j++) { row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString()); sheet.SetColumnWidth(j, 256 * 15); } } //写入数据流 workBook.Write(fs); fs.Flush(); fs.Close(); return true; } /// <summary> /// 导出到Excel /// </summary> /// <param name="table"></param> /// <param name="title"></param> /// <param name="sheetName"></param> /// <returns></returns> public bool ToExcel(DataTable table, string title, string sheetName, string filePath) { this._title = title; this._sheetName = sheetName; this._filePath = filePath; return ToExcel(table); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值