如何導入Excel模板


 

 

protected void DownLoadT_Click(object sender, EventArgs e)
{

  DownloadFileUTF8("評片記錄導入模板表.xlsx", "../dot/評片記錄導入模板表.xlsx", false);
}

第一個參數是文件的名字,第二個參數是文件的路徑,是否需要刪除

就是需要先下載模板,然后根據下載的模板填寫數據,最后上傳Excel,將Excel中的數據導入到數據庫中

 

當我點擊瀏覽,上傳填寫完數據的Excel文檔,此時導入數據的按鈕也可以進行提交,如果需要哪些數據,就選擇復選框進行勾選即可

 

 當選擇一個文件的時候

 

public static DataTable ImportExcelFile(string filePath)
{
  IWorkbook hssfworkbook;
      try{
      using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read)){
     hssfworkbook = new HSSFWorkbook(file);
     file.Close();}
      }
   catch (Exception e){
   using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read))
   {
    hssfworkbook = new XSSFWorkbook(file);
    file.Close();
  }
 }

ISheet sheet = hssfworkbook.GetSheetAt(0);

System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
DataTable dt = new DataTable();
int colnum = sheet.GetRow(0).LastCellNum;
for (int j = 0; j < colnum; j++)
{
  dt.Columns.Add("A" + (j + 1).ToString());
}
rows.MoveNext();
while (rows.MoveNext())
{
  IRow row = (IRow)rows.Current;
  DataRow dr = dt.NewRow();
    for (int i = 0; i < row.LastCellNum && i < colnum; i++) {
    NPOI.SS.UserModel.ICell cell = row.GetCell(i);
   if (cell == null)
{
dr[i] = null;
}
else
{
{
dr[i] = cell.ToString();
}
}
}
dt.Rows.Add(dr);
}

hssfworkbook.Close();

return dt;
}

 

當點擊導入按鈕的時候 

protected void importExcel_Click(object sender, EventArgs e)
{
int num = 0;
foreach (int rowno in Grid1.SelectedRowIndexArray)
{
GridRow row = Grid1.Rows[rowno + Grid1.PageIndex * Grid1.PageSize];
string C1 = row.Values[1].ToString().Trim();
string C2 = row.Values[2].ToString().Trim();
string C3 = row.Values[3].ToString().Trim();
string C4 = row.Values[4].ToString().Trim();
string C5 = row.Values[5].ToString().Trim();
string C6 = row.Values[6].ToString().Trim();
string C7 = row.Values[7].ToString().Trim();
XElement el = new XElement("root"
, new XElement("Type", "Insert")
, new XElement("MainAID", YID.Text)
, new XElement("FormID", FormID.Text)
, new XElement("Text1", C1)
, new XElement("Text2", C2)
, new XElement("Text3", C3)
, new XElement("Text4", C4)
, new XElement("Text5", C5)
, new XElement("Text6", C6)
, new XElement("Text7", C7)
, new XElement("Creator",getUserName()));
bool retVal = Govaze.SQLServerDAL.Factory.getDataXmlDAL().ExecProc(el.ToString(), "DataXmlRec");
if (retVal)
{
num++;
}
else
{
Alert.Show(string.Format("本次共導入{0}個數據!", num));
return;
}
}
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM