Save the
Excel File in the local Drive
try
{
string path = "";
string[] Pathlist = Application.StartupPath.Split('\\');
foreach (var item in Pathlist)
{
if (item.ToString() == "bin")
{
break;
}
else
{ path += item + "\\";
}
}
path += @"FileUploads\Template\DJIL.xlsx";
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Excel
Files (*.xlsx)|*.xlsx|All Files (*.*)|*.*";
saveFileDialog1.FileName = "DJIL.xlsx";
saveFileDialog1.FilterIndex = 1;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string StaticPath = Application.StartupPath
+ "DJIL.xlsx";
FileInfo p = new
FileInfo(saveFileDialog1.FileName.ToString());
if (p.Name == "DJIL.xlsx")
{
if (File.Exists(saveFileDialog1.FileName))
{
System.IO.File.Delete(saveFileDialog1.FileName);
}
System.IO.File.Copy(path,
saveFileDialog1.FileName);
File.SetAttributes(saveFileDialog1.FileName, FileAttributes.Normal);
System.Diagnostics.Process.Start(saveFileDialog1.FileName);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Import Excel
Data To a GridView
OpenFileDialog dlg = new
OpenFileDialog();
dlg.Filter = ("Excel
Files (*.xlsx)|*.xlsx");
DialogResult dlgResult = dlg.ShowDialog();
if (dlgResult == DialogResult.OK)
{
Path = dlg.FileName;
LoadExcelData();
}
private void
LoadExcelData()
{
if (System.IO.File.Exists(Path))
{
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data
Source={0};Extended Properties=""Excel
8.0;HDR=YES;IMEX=1;""", Path);
string query = String.Format("select * from [{0}$]", "Sheet1");
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query,
connectionString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
DataGridView1.DataSource = dataSet.Tables[0];
}
else
{
MessageBox.Show("No
File is Selected");
}
}
After Import
Excel Data To a GridView Save it to DataBase
try
{
POS_StoreRoom objImportItems = new POS_StoreRoom();
;
DataTable dt = new DataTable();
dt.Columns.Add("Item_Name",
typeof(string));
dt.Columns.Add("Variation_Name",
typeof(string));
dt.Columns.Add("Barcode",
typeof(string));
dt.Columns.Add("Quantity", typeof(int));
dt.Columns.Add("Cost", typeof(decimal));
dt.Columns.Add("Price", typeof(decimal));
for (int i = 0; i <
dgvExcelData.RowCount - 1; i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j <
dgvExcelData.ColumnCount; j++)
{
if (dgvExcelData[j, i].Value != null)
{
dr[j] = dgvExcelData[j, i].Value.ToString();
//dt.Rows.Add(dgvExcelData[0, i].Value.ToString(),
dgvExcelData[1, i].Value.ToString(), dgvExcelData[2, i].Value.ToString(), dgvExcelData[3,
i].Value.ToString(), dgvExcelData[4, i].Value.ToString(), dgvExcelData[5,
i].Value.ToString());
}
}
dt.Rows.Add(dr);
}
//dgvExcelData.DataSource = dt;
if (dt.Rows.Count > 0)
{
objImportItems.ImportItems = dt;
ERPManagement.GetInstance.InsertImportItems(objImportItems);
}
}
catch (Exception Ex1)
{
MessageBox.Show(Ex1.ToString(), "Error",
MessageBoxButtons.OK);
}
No comments:
Post a Comment