Permalink
Browse files

修改读取Excel表名的方法。

  • Loading branch information...
1 parent 1b69ee4 commit 676e4f4bcf7390f431cb3e04d7676f724cf70ef6 Henry Yang committed Apr 12, 2012
@@ -62,9 +62,6 @@
<HintPath>Tools\EPPlus.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Build.Engine" />
- <Reference Include="Microsoft.Office.Interop.Excel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
- <EmbedInteropTypes>True</EmbedInteropTypes>
- </Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@@ -3,7 +3,6 @@
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
-using Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace ExcelApplication
@@ -15,23 +14,6 @@ public Form1()
InitializeComponent();
}
- public static List<string> GetSheetNames(string path)
- {
- List<string> sheets = new List<string>();
- Object missing = Missing.Value;
- _Application excel = new Microsoft.Office.Interop.Excel.Application();
- excel.Visible = false;
- Workbooks workbooks = excel.Workbooks;
- workbooks._Open(path, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
- for (int i = 0; i < excel.Worksheets.Count; i++)
- {
- Worksheet sheet = (Worksheet)excel.Worksheets.get_Item(i + 1);
- sheets.Add(sheet.Name.ToString());
- }
- excel.Application.Quit();
- return sheets;
- }
-
public bool dataTotal(int col)
{
try
@@ -89,7 +71,12 @@ private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
return;
}
- comboBox1.DataSource = GetSheetNames(txtPath.Text);
+ ExcelLib.IExcel tmp = ExcelLib.PreExcel.GetExcel(txtPath.Text);
+ if (tmp == null)
+ MessageBox.Show("File Not Found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ if (!tmp.Open())
+ MessageBox.Show("File Not Found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ comboBox1.DataSource = tmp.GetWorkSheets();
btnLoadData.Enabled = true;
}
@@ -196,10 +183,7 @@ private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
}
if (tmp.Save(sheetName, array))
- {
- comboBox1.DataSource = GetSheetNames(txtPath.Text);
MessageBox.Show("File Save Success!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
tmp.Close();
}
catch (Exception ex)
@@ -74,7 +74,7 @@ public bool Open()
throw new Exception("Open file fail,information: " + ex.Message);
}
return true;
- }
+ }
public bool Save(string newSheetName, string[,] array)
{
@@ -113,7 +113,24 @@ public bool Save(string newSheetName, string[,] array)
}
return true;
}
-
+
+ public string[] GetWorkSheets()
+ {
+ string[] sheets = new string[book.Worksheets.Count];
+ try
+ {
+ for (int i = 0; i < book.Worksheets.Count; i++)
+ {
+ sheets[i] = book.Worksheets[i + 1].ToString();
+ }
+ }
+ catch (Exception ex)
+ {
+ throw new Exception("Read Sheets fail,information: " + ex.Message);
+ }
+ return sheets;
+ }
+
public int GetRowCount()
{
if (currentSheet == null) return 0;
@@ -22,6 +22,8 @@ public interface IExcel
/// <param name="newSheetName">新表名</param>
/// <param name="array">需保存的二维数组</param>
bool Save(string newSheetName, string[,] array);
+ /// <summary> 获取当前工作表名称 </summary>
+ string[] GetWorkSheets();
/// <summary> 获取当前工作表中行数 </summary>
int GetRowCount();
/// <summary> 获取当前工作表中列数 </summary>

0 comments on commit 676e4f4

Please sign in to comment.