2013-05-03 83 views
0

嗨簡單的問題,我想進入一個文件夾尋找Excel文件。然後進入每個excel文件,並使用c#將紅色字體顏色更改爲黑色。這可能嗎?是否可以使用c#來訪問excel文件?

namespace Excel_font_color_change 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 

      InitializeComponent(); 
     } 



     private void button1_Click(object sender, EventArgs e) 
     { 
      List<string> HtmlPathList = new List<string>(); 
      string folderToSearch; 

      FolderBrowserDialog fbd = new FolderBrowserDialog(); 
      fbd.ShowNewFolderButton = true;//allow user to create new folders through this dialog 
      fbd.RootFolder = Environment.SpecialFolder.MyDocuments;//defaults to my computer 
      System.Windows.Forms.DialogResult dr = fbd.ShowDialog();//make sure user clicks ok 
      if (dr == DialogResult.OK) 
      { 
       folderToSearch = fbd.SelectedPath;//gets folder path 
       try 
       { 
        var allFiles = from files in Directory.EnumerateFiles(folderToSearch, "*.xls*", SearchOption.AllDirectories) 
            select Path.GetFullPath(files);//gets all files with htm & htm + something for extensions 
        foreach (string filepath in allFiles) 
        { 
         HtmlPathList.Add(filepath);//adds each filepath found to the list 
        } 
       } 
       catch (UnauthorizedAccessException UAEx) { Console.WriteLine(UAEx.Message); }//error handling 
       catch (PathTooLongException PathEx) { Console.WriteLine(PathEx.Message); }//error handling 
       Console.WriteLine("1"); 
      } 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 

     } 
    } 

這是我到目前爲止,我想如果是紅色的第二個按鈕,採取在HtmlPathList文件路徑和編輯字體顏色爲黑色的東西。我現在正在查看How to read data of an Excel file using C#?

+12

對的,這是可能的。 – 2013-05-03 10:34:02

+0

是的。你到目前爲止有多遠?您可以枚舉目錄中的文件,然後使用EPPlus等來打開和修改Excel文件。 – Simon 2013-05-03 10:35:09

+3

任何事情都是可能的......釋放你的思想 – RedEyedMonster 2013-05-03 10:35:41

回答

0

這種解決方案需要對Excel互操作程序集的引用(必須正在執行在後臺執行Excel中的效用作爲互操作程序集在機器上安裝了Excel):

using Microsoft.Office.Interop.Excel; 

    /// <summary> 
    /// sets a cell range's font color 
    /// </summary> 
    /// <param name="filename"></param> 
    /// <param name="startCell"></param> 
    /// <param name="endCell"></param> 
    /// <param name="color"></param> 
    public void setCellRangeFontColor(string filename, string startCell, string endCell, string color) 
    { 
     Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); 

     if (xlApp == null) 
     { 
      MessageBox.Show("EXCEL could not be started. Check that your office installation and project references are correct."); 
      return; 
     } 
     //xlApp.Visible = true; 

     //Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); 
     Workbook wb = xlApp.Workbooks.Open(filename, 
       Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
       Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
       Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
       Type.Missing, Type.Missing); 
     Worksheet ws = (Worksheet)wb.Worksheets[1]; 

     if (ws == null) 
     { 
      MessageBox.Show("Worksheet could not be created. Check that your office installation and project references are correct."); 
     } 

     ws.get_Range(startCell, endCell).Font.Color = System.Drawing.ColorTranslator.ToOle(Color.FromName(color)); 

     wb.Close(true, Type.Missing, Type.Missing); 

     //wb.Save(); 
     xlApp.Quit(); 

     releaseObject(ws); 
     releaseObject(wb); 
     releaseObject(xlApp); 
    } 

    public static void releaseObject(object obj) 
    { 
     try 
     { 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); 
      obj = null; 
     } 
     catch (Exception ex) 
     { 
      obj = null; 
      //MessageBox.Show("Exception Occured while releasing object " + ex.ToString()); 
     } 
     finally 
     { 
      GC.Collect(); 
     } 
    } 
相關問題