C# 操作excel 资料

using System;
using Excel=Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace MailSend.classFile
{
    
/// <summary>
    
/// GetExcel の概要の説明です。
    
/// </summary>

    public class GetExcel
    
{
        
private DataTable _table;
        
private string _fileName;
        
public GetExcel()
        
{
            
        }

        
public GetExcel(DataTable tb,string FileName)
        
{
            _table
=tb;
            _fileName
=FileName;
        }
        

        
public void Down_Excel()
        
{
            
if(System.IO.File.Exists(_fileName))
            
{
                System.IO.File.Delete(_fileName);
            }

                
            
int rowCount=_table.Rows.Count;
            
int coulumnCount=_table.Columns.Count;

            Excel.Application xl
=new Excel.Application();
            Excel.Workbook xbook
=xl.Application.Workbooks.Add(true);
            
//xl.Application.Workbooks.Add(true);
            Excel.Worksheet excelWorkSheet = (Excel.Worksheet)xbook.Worksheets.Add(Missing.Value, Missing.Value, 1, Missing.Value);
            
switch(classFile.Field.MailFlg)
            
{
                
case "1":
                    excelWorkSheet.Name 
= DateTime.Today.ToString("yyyyMMdd")+"ストコン⇔本社売上⇔現金管理";
                    
break;
                
case "2":
                    
//excelWorkSheet.Name = DateTime.Today.ToString("yyyyMMdd")+"ストコン⇔本社売上⇔現金管理";
                    break;
                
case "3":
                    excelWorkSheet.Name 
= DateTime.Today.ToString("yyyyMMdd")+"売上ヘッダー明細整合";
                    
break;
                
default:
                    
//MessageBox.Show("起動バラメータがエラー。");
                    
//Application.Exit();
                    break;
            }
         
            

            
int rowIndex = 2;
            
int colIndex = 1;
            
foreach(DataColumn col in _table.Columns)
            
{
                colIndex
++;
                Excel.Range cell
=(Excel.Range)excelWorkSheet.Cells[2,colIndex];
                cell.Value2 
= col.ColumnName;
                cell.Font.ColorIndex
=5;  //3 red 5 blue 7 pink         
            }
                
                    
//            foreach(DataRow row in _table.Rows)
//            {
//                rowIndex++;
//                colIndex = 1;
//                foreach(DataColumn col in _table.Columns)
//                {
//                    colIndex++;
//                    excelWorkSheet.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
//                }
//            }            
             rowIndex = 0;
             colIndex 
= 0;            
            
string [,] array=new string[rowCount,coulumnCount];
            
try
            
{
                
foreach(DataRow row in _table.Rows)
                
{
                
                    
foreach(DataColumn col in _table.Columns)
                    
{                        
                        array[rowIndex,colIndex]
=row[col.ColumnName].ToString();
                        colIndex
++;
                    }

                    rowIndex
++;
                    colIndex 
= 0;
                }

            }

            
catch(Exception ex)
            
{
                MessageBox.Show(ex.Message);
            }


            Excel.Range dataRange
=excelWorkSheet.get_Range(excelWorkSheet.Cells[3,2],excelWorkSheet.Cells[rowCount+2,coulumnCount+1]);
            dataRange.Value2
=array;

            
            
                
            Excel.Range select
=excelWorkSheet.get_Range(excelWorkSheet.Cells[2,2],excelWorkSheet.Cells[2,coulumnCount+1]);
            select.Interior.ColorIndex
=6;//backgroud
            select.EntireColumn.AutoFit();
            select.EntireRow.AutoFit();     
            select.HorizontalAlignment
=Excel.XlHAlign.xlHAlignCenter;        
            select.VerticalAlignment 
= Excel.XlVAlign.xlVAlignCenter; //
            select.Borders.LineStyle = BorderStyle.None;
            select.Borders.Weight 
= Excel.XlBorderWeight.xlThin;
            select.Borders.ColorIndex 
= Excel.XlColorIndex.xlColorIndexAutomatic;

            select
=excelWorkSheet.get_Range(excelWorkSheet.Cells[3,2],excelWorkSheet.Cells[rowCount+2,4]);
            select.EntireColumn.AutoFit();
            select.EntireRow.AutoFit();     
            select.HorizontalAlignment
=Excel.XlHAlign.xlHAlignLeft;
            select.VerticalAlignment 
= Excel.XlVAlign.xlVAlignCenter; 
            select.Borders.LineStyle 
= BorderStyle.None;
            select.Borders.Weight 
= Excel.XlBorderWeight.xlThin;
            select.Borders.ColorIndex 
= Excel.XlColorIndex.xlColorIndexAutomatic;

            select
=excelWorkSheet.get_Range(excelWorkSheet.Cells[3,4],excelWorkSheet.Cells[rowCount+2,coulumnCount+1]);
            select.EntireColumn.AutoFit();
            select.EntireRow.AutoFit();     
            select.HorizontalAlignment
=Excel.XlHAlign.xlHAlignRight;
            select.VerticalAlignment 
= Excel.XlVAlign.xlVAlignCenter; 
            select.Borders.LineStyle 
= BorderStyle.None;
            select.Borders.Weight 
= Excel.XlBorderWeight.xlThin;
//            select.Borders.ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;                        
                
            xl.Visible 
= false;
            xbook.Saved 
= true;
            xl.DisplayAlerts
=false;
            xbook.SaveCopyAs(_fileName);

            
//xl.Visible = false;
            
//                if(System.IO.File.Exists(FilePath))
            
//                {
            
//                    System.IO.File.Delete(FilePath);
            
//                }
            
//xl.DisplayAlerts=false;        

            
//                Excel.Worksheet xSheet=(Excel.Worksheet)xbook.Sheets[1]; 
            
//                xbook.SaveAs(FilePath,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value); 
            
//                
            
//xl.ActiveWorkbook.SaveAs(FilePath,Excel.XlFileFormat.xlExcel9795,null,null,false,false,Excel.XlSaveAsAccessMode.xlNoChange,null,null,null,null,null);
                
            xl.Quit();                
            excelWorkSheet
=null;
            xbook
=null;
            xl
=null;
            select
=null;
            GC.Collect(); 
// ゴミ回収
            
//}
        }

        

    }

}

posted @ 2008-05-12 12:50  TerryLove  阅读(174)  评论(0编辑  收藏  举报