C#讀取excel資料
阿新 • • 發佈:2019-01-01
C#讀取和寫入excel資料
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Reflection; using Microsoft.Office.Interop.Excel; namespace test1 { class Program { public static Array ReadXls(string filename, int index)//讀取第index個sheet的資料 { //啟動Excel應用程式 Microsoft.Office.Interop.Excel.Application xls = new Microsoft.Office.Interop.Excel.Application(); //開啟filename表 _Workbook book = xls.Workbooks.Open(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); _Worksheet sheet;//定義sheet變數 xls.Visible = false;//設定Excel後臺執行 xls.DisplayAlerts = false;//設定不顯示確認修改提示 try { sheet = (_Worksheet)book.Worksheets.get_Item(index);//獲得第index個sheet,準備讀取 } catch (Exception ex)//不存在就退出 { Console.WriteLine(ex.Message); return null; } Console.WriteLine(sheet.Name); int row = sheet.UsedRange.Rows.Count;//獲取不為空的行數 int col = sheet.UsedRange.Columns.Count;//獲取不為空的列數 // Array value = (Array)sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[row, col]).Cells.Value2;//獲得區域資料賦值給Array陣列,方便讀取 Microsoft.Office.Interop.Excel.Range range = sheet.Range[sheet.Cells[1,1],sheet.Cells[row,col]]; Array value = (Array)range.Value2; book.Save();//儲存 book.Close(false, Missing.Value, Missing.Value);//關閉開啟的表 xls.Quit();//Excel程式退出 //sheet,book,xls設定為null,防止記憶體洩露 sheet = null; book = null; xls = null; GC.Collect();//系統回收資源 return value; } static void Main(string[] args) { string Current; Current = Directory.GetCurrentDirectory();//獲取當前根目錄 Array Data = ReadXls(Current + "\\test3.xlsx", 1);//讀取test.xlsx的第一個sheet表 foreach (string temp in Data) Console.WriteLine(temp); Console.ReadKey(); } /*--------------------------------------------------------將資料寫入excel------------------------------------------------------------------ public static bool WriteXls(string filename,string a) { //啟動Excel應用程式 Microsoft.Office.Interop.Excel.Application xlsx = new Microsoft.Office.Interop.Excel.Application(); _Workbook book = xlsx.Workbooks.Add(Missing.Value); //建立一張表,一張表可以包含多個sheet //如果表已經存在,可以用下面的命令開啟 //_Workbook book = xls.Workbooks.Open(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); _Worksheet sheet;//定義sheet變數 xlsx.Visible = false;//設定Excel後臺執行 xlsx.DisplayAlerts = false;//設定不顯示確認修改提示 for (int i = 1; i < 4; i++)//迴圈建立並寫入資料到sheet { try { sheet = (_Worksheet)book.Worksheets.get_Item(i);//獲得第i個sheet,準備寫入 } catch (Exception ex)//不存在就增加一個sheet { sheet = (_Worksheet)book.Worksheets.Add(Missing.Value, book.Worksheets[book.Sheets.Count], 1, Missing.Value); } sheet.Name = "第" + i.ToString() + "頁";//設定當前sheet的Name for (int row = 1; row < 20; row++)//迴圈設定每個單元格的值 { for (int offset = 1; offset < 10; offset++) sheet.Cells[row, offset] = "( " + row.ToString() + "," + offset.ToString() + " )"+a; } } //將表另存為 book.SaveAs(filename, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); //如果表已經存在,直接用下面的命令儲存即可 //book.Save(); book.Close(false, Missing.Value, Missing.Value);//關閉開啟的表 xlsx.Quit();//Excel程式退出 //sheet,book,xls設定為null,防止記憶體洩露 sheet = null; book = null; xlsx = null; GC.Collect();//系統回收資源 return true; } static void Main(string[] args) { string Current,zqh="zqh"; Current = Directory.GetCurrentDirectory();//獲取當前根目錄 WriteXls(Current + "\\test.xlsx",zqh); } */ /*--------------------------------------------------------------------將資料寫入text------------------------------------------------------------------------- static void Main(string[] args) { StreamWriter sw = new StreamWriter("f:\\C#\\temp4.txt", true, Encoding.GetEncoding("gb2312")); string data = "hello world"; sw.WriteLine(data); int[] a = {1,2,3,4,5,6}; sw.WriteLine(a.Length); for(int i=0;i<a.Length;i++) { sw.WriteLine(a[i]); } //sw.Close(); while (true) { string email, email0 = "請輸入你的電子郵箱"; Console.WriteLine(email0); email = Console.ReadLine(); sw.WriteLine(email0); sw.WriteLine(email); Console.WriteLine("你的郵箱是{0}", email); Console.WriteLine("繼續輸入郵箱嗎?"); string input = Console.ReadLine(); sw.WriteLine(input); if (input.ToUpper() == "YES") continue; else break; } sw.Close(); } */ } }
引用好Microsoft.Office.Interop.Excel可以直接讀取,
原來用的這句話:
// Array value = (Array)sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[row, col]).Cells.Value2;//獲得區域資料賦值給Array陣列,方便讀取
一直報錯,說object未包含get_range定義,後來改成下面的語句,可以了
Microsoft.Office.Interop.Excel.Range range = sheet.Range[sheet.Cells[1,1],sheet.Cells[row,col]]; Array value = (Array)range.Value2;
還有一點:如果excel中的內容資料格式不同,可以用var型別,比如:
foreach (var temp in Data)
Console.WriteLine(temp);