|
|
|
C# อยากทราบวิธีอ่าน File Excel แล้วให้มันเก็บค่าที่ได้ไว้ใน Array |
|
|
|
|
|
|
|
ใน file Excel ของผมมีแค่
1 0 0 1 0 1 1
0 1 1 1 0 1 0
1 0 0 1 0 1 1
0 1 1 1 0 1 0
นี้อะครับ
|
|
|
|
|
Date :
2012-12-19 11:05:31 |
By :
lilostitchs |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MsExcel = Microsoft.Office.Interop.Excel;
namespace MsExcelInterop
{
class ExcelManagement : IDisposable
{
private MsExcel.Application _xlApp;
private MsExcel.Workbook _xlBook;
public ExcelManagement (string FilePath)
{
_xlApp = new MsExcel.Application();
_xlApp.Visible = false;
_xlApp.ScreenUpdating = false;
_xlApp.DisplayAlerts = false;
_xlBook = _xlApp.Workbooks.Open(FilePath, 0, true, 5, Type.Missing, Type.Missing, true, MsExcel.XlPlatform.xlWindows, "\\t", false, false, 0, true, true, Type.Missing);
}
public List<object[,]> GetSheetData()
{
List<object[,]> sheetData = new List<object[,]>();
foreach (MsExcel.Worksheet xlSheet in _xlBook.Sheets)
{
MsExcel.Range xlRange = xlSheet.UsedRange;
object[,] valueArray = (object[,])xlRange.get_Value(MsExcel.XlRangeValueDataType.xlRangeValueDefault);
sheetData.Add(valueArray);
}
return sheetData;
}
public void Dispose()
{
if (_xlApp != null)
{
_xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(_xlBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(_xlApp);
GC.SuppressFinalize(_xlBook);
GC.SuppressFinalize(_xlApp);
GC.Collect();
_xlBook = null;
_xlApp = null;
}
}
}
}
เวลาใช้
Code (C#)
using (MsExcelInterop.ExcelManagement excel = new MsExcelInterop.ExcelManagement(openFileDialog1.FileName))
{
object[,] valueArray = excel.GetSheetData().First();
}
|
|
|
|
|
Date :
2012-12-19 13:22:54 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับ
|
|
|
|
|
Date :
2012-12-19 22:29:27 |
By :
lilostitchs |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|