Codebehind C#:
private void btnBrowse_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select file";
fdlg.InitialDirectory = @"c:\";
fdlg.FileName = txtFileName.Text;
fdlg.Filter = "Excel Sheet(*.xls)|*.xls|All Files(*.*)|*.*";
fdlg.FilterIndex = 1;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = fdlg.FileName;
Import();
Application.DoEvents();
}
}
catch (Exception ex)
{
ErrorLog.WriteErrorLog(ErrorLog.GetLogFilePath(), ex);
}
}
private void Import()
{
if (txtFileName.Text.Trim() != string.Empty)
{
try
{
string[] strTables = GetTableExcel(txtFileName.Text);
frmSelectTables objSelectTable = new frmSelectTables(strTables);
objSelectTable.ShowDialog(this);
objSelectTable.Dispose();
if ((SelectedTable != string.Empty) && (SelectedTable != null))
{
dt = GetDataTableExcel(txtFileName.Text, SelectedTable);
// Initialize the DataGridView.
dgvUsers.DataSource = null;
dgvUsers.Rows.Clear();
dgvUsers.Columns.Clear();
dgvUsers.AutoGenerateColumns = false;
dgvUsers.AutoSize = true;
dgvUsers.DataSource = dt.DefaultView;
lblMsg.Text = "Total Number :" + dt.Rows.Count;
count = dt.Rows.Count;
lblMsg.Visible = true;
if (count > 240)
{
txtserialnos.Text = "0-240";
}
else
{
txtserialnos.Text = "0-" + (count).ToString();
}
for (int i = 0; i <= dt.Columns.Count; i++)
{
if (i == 0)
{
DataGridViewCheckBoxColumn CBColumn = new DataGridViewCheckBoxColumn();
CBColumn.HeaderText = "";
CBColumn.FalseValue = "0";
CBColumn.TrueValue = "1";
dgvUsers.Columns.Insert(0, CBColumn);
}
else
{
// Initialize and add a text box column.
DataGridViewColumn column = new DataGridViewTextBoxColumn();
column.DataPropertyName = dt.Columns[i - 1].ColumnName;
column.Name = dt.Columns[i - 1].ColumnName;
dgvUsers.Columns.Insert(i, column);
}
}
}
}
catch (Exception ex)
{
ErrorLog.WriteErrorLog(ErrorLog.GetLogFilePath(), ex);
}
}
}
public static DataTable GetDataTableExcel(string strFileName, string Table)
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + strFileName + "; Extended Properties = \"Excel 12.0 Xml;HDR=Yes;IMEX=1\";");
conn.Open();
string strQuery = "SELECT * FROM [" + Table + "]";
//
OleDbDataAdapter adapter = new OleDbDataAdapter(strQuery, conn);
System.Data.DataSet ds = new System.Data.DataSet();
// DataTable dt = new DataTable();
adapter.Fill(ds);
return ds.Tables[0];
}
public static string[] GetTableExcel(string strFileName)
{
string[] strTables = new string[100];
Catalog oCatlog = new Catalog();
ADOX.Table oTable = new ADOX.Table();
ADODB.Connection oConn = new ADODB.Connection();
oConn.Open("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + strFileName + "; Extended Properties = \"Excel 12.0 Xml;HDR=Yes;IMEX=1\";", "", "", 0);
oCatlog.ActiveConnection = oConn;
if (oCatlog.Tables.Count > 0)
{
int item = 0;
foreach (ADOX.Table tab in oCatlog.Tables)
{
if (tab.Type == "TABLE")
{
strTables[item] = tab.Name;
item++;
}
}
}
return strTables;
}
private void ExcelLabel_Load(object sender, EventArgs e)
{
this.reportViewer1.RefreshReport();
}
private void dgvUsers_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
try
{
Point cur = new Point(e.ColumnIndex, e.RowIndex);
// Change the diagonal checkbox to the opposite state
if ((bool)(dgvUsers[cur.X, cur.Y].Selected) == true && (dgvUsers[cur.X, cur.Y].Value.ToString() != "0"))
{
dgvUsers[cur.X, cur.Y].Selected = true;
dgvUsers[cur.X, cur.Y].Value = true;
flag = true;
}
else
{
dgvUsers[cur.X, cur.Y].Selected = false;
dgvUsers[cur.X, cur.Y].Value = false;
flag = true;
}
}
catch (Exception ex)
{
ErrorLog.WriteErrorLog(ErrorLog.GetLogFilePath(), ex);
}
}
private void dgvUsers_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
try
{
if (dgvUsers.IsCurrentCellDirty)
{
dgvUsers.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
catch (Exception ex)
{
ErrorLog.WriteErrorLog(ErrorLog.GetLogFilePath(), ex);
}
}
private void btnReport_Click(object sender, EventArgs e)
{
try
{
DataTable dt1 = null;
dt1 = dt.Clone();
DataRow dr = null;
int ii = 0;
int jj = 0;
dt1.Rows.Clear();
if (chkSerialNos.Checked == true)
{
if (txtserialnos.Text != "")
{
string[] Nos = txtserialnos.Text.Split('-');
for (ii = int.Parse(Nos[0]); ii <= int.Parse(Nos[1]) - 1; ii++)
{
if (flag == false)
{
dr = dt1.NewRow();
for (jj = 0; jj <= dgvUsers.Columns.Count - 2; jj++)
{
dr[jj] = dgvUsers.Rows[ii].Cells[jj + 1].Value;
}
dt1.Rows.Add(dr);
}
else
{
if (dgvUsers.Rows[ii].Cells[0].Value != null)
{
if (dgvUsers.Rows[ii].Cells[0].Value.ToString() != "0")
{
if ((bool)dgvUsers.Rows[ii].Cells[0].Value == true)
{
dr = dt1.NewRow();
for (jj = 0; jj <= dgvUsers.Columns.Count - 2; jj++)
{
dr[jj] = dgvUsers.Rows[ii].Cells[jj + 1].Value;
}
dt1.Rows.Add(dr);
}
}
}
}
}
if ((int.Parse(Nos[1]) + 240) > count)
{
if (int.Parse(Nos[1]) + 1 < count)
{
txtserialnos.Text = (int.Parse(Nos[1]) + 1).ToString() + "-" + count.ToString();
}
}
else
{
txtserialnos.Text = (int.Parse(Nos[1]) + 1).ToString() + "-" + (int.Parse(Nos[1]) + 240).ToString();
}
}
}
else
{
for (ii = 0; ii <= dgvUsers.Rows.Count - 1; ii++)
{
if (flag == false)
{
dr = dt1.NewRow();
for (jj = 0; jj <= dgvUsers.Columns.Count - 2; jj++)
{
dr[jj] = dgvUsers.Rows[ii].Cells[jj + 1].Value;
}
dt1.Rows.Add(dr);
}
else
{
if (dgvUsers.Rows[ii].Cells[0].Value != null)
{
dr = dt1.NewRow();
for (jj = 0; jj <= dgvUsers.Columns.Count - 2; jj++)
{
dr[jj] = dgvUsers.Rows[ii].Cells[jj + 1].Value;
}
dt1.Rows.Add(dr);
}
}
}
}
dt1.Columns[0].ColumnName = "COL1";
dt1.Columns[1].ColumnName = "COL2";
dt1.Columns[2].ColumnName = "COL3";
dt1.Columns[3].ColumnName = "COL4";
dt1.Columns[4].ColumnName = "COL5";
dt1.Columns[5].ColumnName = "COL6";
dt1.Columns[6].ColumnName = "COL7";
dt1.Columns[7].ColumnName = "COL8";
dt1.AcceptChanges();
FillReport(dt1);
}
catch (Exception ex)
{
ErrorLog.WriteErrorLog(ErrorLog.GetLogFilePath(), ex);
}
}
private void FillReport(DataTable dt)
{
reportViewer1.Clear();
reportViewer1.ProcessingMode = ProcessingMode.Local;
reportViewer1.LocalReport.ReportPath = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\ExcelLabels.rdlc";
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(new ReportDataSource(reportViewer1.LocalReport.GetDataSourceNames()[0], dt));
this.reportViewer1.RefreshReport();
tabControl1.SelectTab("tabPage2");
tabControl1.SelectedIndex = 1;
}
private void chkAll_CheckedChanged(object sender, EventArgs e)
{
try
{
if (chkAll.Checked == true)
{
for (int ii = 0; ii <= dgvUsers.Rows.Count - 1; ii++)
{
dgvUsers[0, ii].Selected = true;
dgvUsers[0, ii].Value = true;
}
}
else
{
for (int ii = 0; ii <= dgvUsers.Rows.Count - 1; ii++)
{
dgvUsers[0, ii].Selected = false;
dgvUsers[0, ii].Value = false;
}
}
}
catch (Exception ex)
{
ErrorLog.WriteErrorLog(ErrorLog.GetLogFilePath(), ex);
}
}