Example #1
0
 public static ClsExcel Instance()
 {
     if (clsExcel == null)
     {
         lock (lockHelper)
         {
             if (clsExcel == null)
             {
                 clsExcel = new ClsExcel();
             }
         }
     }
     return(clsExcel);
 }
Example #2
0
        private void btnLoad_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter           = "Excel2010|*.xlsx|Excel2003|*.xls|所有文件|*.*";
                openFileDialog.RestoreDirectory = true;
                openFileDialog.FilterIndex      = 1;

                if (openFileDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                string fName = openFileDialog.FileName;

                ClsExcel clsExcel = ClsExcel.Instance();

                string sSQL = "select * from [Sheet1$]";

                DataTable  dt = clsExcel.ExcelToDT(fName, sSQL, true);
                DataColumn dc = new DataColumn();
                dc.ColumnName = "choose";
                dc.DataType   = System.Type.GetType("System.Boolean");
                dt.Columns.Add(dc);

                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    string sColName = dt.Columns[i].ColumnName;
                    sColName = sColName.Replace(" ", "");
                    sColName = sColName.Replace(".", "");
                    sColName = sColName.Replace("#", "");

                    dt.Columns[i].ColumnName = sColName;
                }

                gridControl1.DataSource = dt;

                gridView1.BestFitColumns();

                chkAll.Checked = false;
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }