/// <summary>
/// 导入excel表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnImport_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDlg = new OpenFileDialog();
openFileDlg.Title = "请选择行政区代码与带号映射关系表";
openFileDlg.Filter = "工作薄(*.xls;*.xlsx)|*.xls|(*.xlsx)|*.xlsx";
if (openFileDlg.ShowDialog() != DialogResult.OK)
{
return;
}
string strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;", openFileDlg.FileName);
OleDbConnection conn = new OleDbConnection(strConn);
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [Sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
DataTable table1 = new DataTable();
ds = new DataSet();
try
{
myCommand.Fill(ds, "table1");
}
catch (System.Exception)
{
XtraMessageBox.Show("请关闭当前工作的Excel表!");
DebugTool.yDebugStrTime("打开excel表失败");
return;
}
DataTable dt = ds.Tables[0];
int nRowCount = dt.Rows.Count;
this.gridcRegionZone.DataSource = dt;
for (int j = 0; j < nRowCount; j++)//读取excel表
{
if (dt.Rows[j][COL_REGIONCODE].ToString() == "" && dt.Rows[j][COL_REGIONNAME].ToString() == "" && dt.Rows[j][COL_ZONE].ToString() == "")//遇到空白说明结束
{
break;
}
string strRegionCode= dt.Rows[j][COL_REGIONCODE].ToString();
string strRegionName = dt.Rows[j][COL_REGIONNAME].ToString();
string strZone = dt.Rows[j][COL_ZONE].ToString();
m_rz_dt.Rows.Add(new object[] { strRegionCode, strRegionName, strZone });
}
this.gridcRegionZone.DataSource = m_rz_dt;
this.gvRegionZone.Columns[0].MaxWidth = 100;
this.gvRegionZone.Columns[1].MaxWidth = 100;
this.gvRegionZone.Columns[2].MaxWidth = 100;
}
网友评论