private void sb_import_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); DataTable dt = null; OpenFileDialog sflg = new OpenFileDialog(); sflg.Filter = "Excel(*.xls)|*.xls|Excel(*.xlsx)|*.xlsx"; if (sflg.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) { return; } FileStream fs = new FileStream(sflg.FileName, FileMode.Open, FileAccess.Read); NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(fs); int sheetCount = book.NumberOfSheets; for (int sheetIndex = 0; sheetIndex < sheetCount; sheetIndex++) { string st_name = book.GetSheetName(sheetIndex); XtraTabPage xinka = new XtraTabPage(); xinka.Name = "xin"; xinka.Text = st_name; NepCalaTable xintab = new NepCalaTable(); xintab.Dock = DockStyle.Fill; xinka.Controls.Add(xintab); this.xtraTabControl1.TabPages.Add(xinka); this.xtraTabControl1.SelectedTabPage = xinka; this.active_nepCalaTable = xintab; NPOI.SS.UserModel.ISheet sheet = book.GetSheetAt(sheetIndex); if (sheet == null) { continue; } NPOI.SS.UserModel.IRow row = sheet.GetRow(0); if (row == null) { continue; } int firstCellNum = row.FirstCellNum; int lastCellNum = row.LastCellNum; if (firstCellNum == lastCellNum) { continue; } dt = new DataTable(sheet.SheetName); dt.Columns.Add("bushe_xianshu", typeof(int)); //MessageBox.Show(dt.Columns["bushe_xianshu"].DataType.ToString()); dt.Columns.Add("bushe_daoshu", typeof(int)); dt.Columns.Add("bushe_zongdaoshu", typeof(int)); dt.Columns.Add("banqian_daoshu", typeof(int)); dt.Columns.Add("ke_caiji", typeof(int)); dt.Columns.Add("banjia_daoshu", typeof(int)); dt.Columns.Add("hengxiangchang", typeof(int)); dt.Columns.Add("zongxiangchang", typeof(int)); dt.Columns.Add("zonghengbi", typeof(double)); dt.Columns.Add("paodaobi", typeof(double)); lastCellNum = 10; for (int i = firstCellNum; i < lastCellNum; i++) { dt.Columns.Add(row.GetCell(i).StringCellValue, typeof(string)); } for (int i = 1; i <= sheet.LastRowNum; i++) { DataRow newRow = dt.Rows.Add(); for (int j = firstCellNum; j < lastCellNum; j++) { newRow[j] = sheet.GetRow(i).GetCell(j).StringCellValue; } } NPOI.SS.UserModel.IRow row0 = sheet.GetRow(0); this.active_nepCalaTable.jifa_dianju.Text = row0.GetCell(13).StringCellValue; row0 = sheet.GetRow(1); this.active_nepCalaTable.jieshou_dianju.Text = row0.GetCell(13).StringCellValue; row0 = sheet.GetRow(2); this.active_nepCalaTable.jieshou_dianshu.Text = row0.GetCell(13).StringCellValue; row0 = sheet.GetRow(3); this.active_nepCalaTable.jifa_xianju.Text = row0.GetCell(13).StringCellValue; row0 = sheet.GetRow(4); this.active_nepCalaTable.jieshou_xianju.Text = row0.GetCell(13).StringCellValue; row0 = sheet.GetRow(5); this.active_nepCalaTable.muban_zong.Text = row0.GetCell(13).StringCellValue; row0 = sheet.GetRow(6); this.active_nepCalaTable.mobanpao.Text = row0.GetCell(13).StringCellValue; row0 = sheet.GetRow(7); this.active_nepCalaTable.jieshou_xianshu.Text = row0.GetCell(13).StringCellValue; row0 = sheet.GetRow(8); this.active_nepCalaTable.muban_heng.Text = row0.GetCell(13).StringCellValue; row0 = sheet.GetRow(9); this.active_nepCalaTable.bushe_jieshouxianshu.Text = row0.GetCell(13).StringCellValue; row0 = sheet.GetRow(10); this.active_nepCalaTable.bushe_jifaxianshu.Text = row0.GetCell(13).StringCellValue; ds.Tables.Add(dt); this.active_nepCalaTable.DDT = dt; } }
private void import_bt_Click(object sender, EventArgs e) { //string filepath = ""; //OpenFileDialog opf = new OpenFileDialog(); //if (opf.ShowDialog() == DialogResult.OK) //{ // filepath = opf.FileName; //} DataSet ds = new DataSet(); System.Data.DataTable dt = null; OpenFileDialog sflg = new OpenFileDialog(); sflg.Filter = "Excel(*.xls)|*.xls|Excel(*.xlsx)|*.xlsx"; if (sflg.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) { return; } FileStream fs = new FileStream(sflg.FileName, FileMode.Open, FileAccess.Read); NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(fs); int sheetCount = book.NumberOfSheets; for (int sheetIndex = 0; sheetIndex < sheetCount; sheetIndex++) { string st_name = book.GetSheetName(sheetIndex); NPOI.SS.UserModel.ISheet sheet = book.GetSheetAt(sheetIndex); if (sheet == null) { continue; } NPOI.SS.UserModel.IRow row = sheet.GetRow(0); if (row == null) { continue; } int firstCellNum = row.FirstCellNum; int lastCellNum = row.LastCellNum; if (firstCellNum == lastCellNum) { continue; } dt = new System.Data.DataTable(sheet.SheetName); dt.Columns.Add("PN", typeof(string)); //MessageBox.Show(dt.Columns["bushe_xianshu"].DataType.ToString()); //dt.Columns.Add("", typeof(int)); dt.Columns.Add("name", typeof(string)); dt.Columns.Add("jobnum", typeof(string)); dt.Columns.Add("ARef", typeof(string)); dt.Columns.Add("size", typeof(string)); dt.Columns.Add("sm", typeof(string)); dt.Columns.Add("Barcode", typeof(string)); lastCellNum = 7; for (int i = firstCellNum; i < lastCellNum; i++) { dt.Columns.Add(row.GetCell(i).StringCellValue, typeof(string)); } for (int i = 1; i <= sheet.LastRowNum; i++) { DataRow newRow = dt.Rows.Add(); for (int j = firstCellNum; j < lastCellNum; j++) { newRow[j] = sheet.GetRow(i).GetCell(j).StringCellValue; } } NPOI.SS.UserModel.IRow row0 = sheet.GetRow(0); ds.Tables.Add(dt); main_gc.DataSource = ds.Tables[0]; } for (int i = 0; i < gridView1.RowCount; i++) { string LJH = gridView1.GetRowCellValue(i, "PN").ToString(); string mc = gridView1.GetRowCellValue(i, "name").ToString(); string gdh = gridView1.GetRowCellValue(i, "jobnum").ToString(); string BH = gridView1.GetRowCellValue(i, "ARef").ToString(); string cc = gridView1.GetRowCellValue(i, "size").ToString(); string dsm = gridView1.GetRowCellValue(i, "sm").ToString(); string tm = gridView1.GetRowCellValue(i, "Barcode").ToString(); Maticsoft.BLL.parts use = new Maticsoft.BLL.parts(); Maticsoft.Model.parts us = new parts() { PN = LJH, //name = mc, //jobnum = gdh, //ARef = BH, //size = cc, //sm = dsm, Barcode = tm, }; use.Add(us); } DevExpress.XtraEditors.XtraMessageBox.Show("导入成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); //MessageBox.Show("已成功导入"); Maticsoft.BLL.parts pr = new Maticsoft.BLL.parts(); DataSet ds2 = pr.GetAllList(); main_gc.DataSource = ds2.Tables[0]; }