Permalink
Browse files

IExcel

  • Loading branch information...
1 parent 821141c commit f3c78b25d98995567b752a94418fca9e8c7099cc Advansil committed Apr 19, 2012

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -14,6 +14,7 @@ public Form1()
InitializeComponent();
}
+ #region Methods
public bool dataTotal(int col)
{
try
@@ -91,6 +92,8 @@ public bool dataTotal(int col)
return false;
}
+ #endregion
+
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
@@ -104,17 +107,20 @@ private void openToolStripMenuItem_Click(object sender, EventArgs e)
if (dlgResult == DialogResult.OK)
{
txtPath.Text = dlg.FileName;
+ ExcelLib.IExcel tmp = ExcelLib.PreExcel.GetExcel(txtPath.Text);
+ if (tmp == null & !tmp.Open())
+ {
+ MessageBox.Show("File Not Found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ return;
+ }
+ comboBox1.DataSource = tmp.GetWorkSheets();
+ tmp.Close();
+ btnLoadData.Enabled = true;
}
else
{
return;
- }
- 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();
+ }
}
private void calculatorToolStripMenuItem_Click(object sender, EventArgs e)
@@ -137,53 +143,54 @@ private void btnLoadData_Click(object sender, EventArgs e)
try
{
ExcelLib.IExcel tmp = ExcelLib.PreExcel.GetExcel(txtPath.Text);
- if (tmp == null)
- MessageBox.Show("File Not Found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
- if (!tmp.Open())
+ if (tmp == null & !tmp.Open())
+ {
MessageBox.Show("File Not Found!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ return;
+ }
tmp.CurrentSheetIndex = comboBox1.SelectedIndex;
int columnCount = tmp.GetColumnCount();
dataGridView1.ColumnCount = columnCount;
int rowCount = tmp.GetRowCount() - 1;
dataGridView1.RowCount = rowCount;
- for (int j = 0; j < columnCount; j++)
+ for (int i = 0; i < columnCount; i++)
{
- dataGridView1.Columns[j].HeaderCell.Value = tmp.GetCellValue(2, j + 1);
+ dataGridView1.Columns[i].HeaderCell.Value = tmp.GetCellValue(2, i + 1);
}
- for (int i = 0; i < rowCount-1; i++)
+ for (int j = 0; j < rowCount-1; j++)
{
- for (int j = 0; j < columnCount; j++)
+ for (int i = 0; i < columnCount; i++)
{
- if (string.IsNullOrEmpty(tmp.GetCellValue(i + 3, j + 1)))
+ if (string.IsNullOrEmpty(tmp.GetCellValue(j + 3, i + 1)))
{
- dataGridView1.Rows[i].Cells[j].Value = "0";
+ dataGridView1.Rows[j].Cells[i].Value = "0";
}
else
{
- dataGridView1.Rows[i].Cells[j].Value = tmp.GetCellValue(i + 3, j + 1);
+ dataGridView1.Rows[j].Cells[i].Value = tmp.GetCellValue(j + 3, i + 1);
}
}
}
tmp.Close();
- string[] rowValue = new string[rowCount];
- for (int i = 0; i < rowCount - 1; i++)
- {
- rowValue[i] = (string)dataGridView1.Rows[i].Cells[1].Value;
- rowValue[i] = rowValue[i].TrimStart('M');
- dataGridView1.Rows[i].Cells[2].Value = (rowValue[i].Length > 6) ? rowValue[i].Substring(0, 6) : rowValue[i];
- }
- dataGridView1.Columns[2].HeaderCell.Value = "铸锭编号";
+ //string[] rowValue = new string[rowCount];
+ //for (int i = 0; i < rowCount - 1; i++)
+ //{
+ // rowValue[i] = (string)dataGridView1.Rows[i].Cells[1].Value;
+ // rowValue[i] = rowValue[i].TrimStart('M');
+ // dataGridView1.Rows[i].Cells[2].Value = (rowValue[i].Length > 6) ? rowValue[i].Substring(0, 6) : rowValue[i];
+ //}
+ //dataGridView1.Columns[2].HeaderCell.Value = "铸锭编号";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
- btnLoadData.Enabled = false;
+ //btnLoadData.Enabled = false;
}
private void codeToolStripMenuItem_Click(object sender, EventArgs e)
@@ -316,7 +323,7 @@ private void cuttingToolStripMenuItem_Click(object sender, EventArgs e)
MessageBox.Show("Processed Data Success!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
- private void cutNOToolStripMenuItem_Click(object sender, EventArgs e)
+ private void cutNoToolStripMenuItem_Click(object sender, EventArgs e)
{
int selectCol = 5;
if (dataTotal(selectCol))
@@ -4,30 +4,31 @@ namespace ExcelLib
{
class PreExcel
{
- /// <param name="filePath">Excel文件路径</param>
+ /// <summary>
+ /// 文件类型检查
+ /// </summary>
+ /// <param name="filePath">路径和文件名</param>
/// <returns></returns>
public static IExcel GetExcel(string filePath)
{
- if (filePath.Trim() == "")
- throw new Exception("文件名不能为空");
-
- if (!filePath.Trim().EndsWith("xls") && !filePath.Trim().EndsWith("xlsx"))
- throw new Exception("不支持该文件类型");
-
- if (filePath.Trim().EndsWith("xls"))
+ try
{
- //IExcel res = new Excel03(filePath.Trim());
- //return res;
- throw new Exception("不支持Excel97~2003版本文件");
- //TODO:未写代码
+ if (filePath.Trim() != "")
+ {
+ IExcel res = null;
+ if (filePath.Trim().EndsWith("xlsx"))
+ res = new Excel2007(filePath.Trim());
+ //else if (filePath.Trim().EndsWith("xls"))
+ // res = new Excel2003(filePath.Trim());
+ return res;
+ }
+ else
+ return null;
}
- else if (filePath.Trim().EndsWith("xlsx"))
+ catch (Exception ex)
{
- IExcel res = new Excel2007(filePath.Trim());
- return res;
+ throw new Exception(ex.Message);
}
- else
- return null;
}
}
}

0 comments on commit f3c78b2

Please sign in to comment.