问题描述
- 用npoi将excel数据导入到数据库,数据0.9变成了.9怎么办
-
DataTable table = new DataTable(); OpenFileDialog opfile = new OpenFileDialog(); opfile.Filter = " xlsx files(*.xls)|*.xls|All files(*.*)|*.*";//选择保存的格式(显示格式|实际格式) if (opfile.ShowDialog() == DialogResult.OK) { string fileName = opfile.FileName;//获取路径 FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read); // XSSFWorkbook hssfw = new XSSFWorkbook(file); HSSFWorkbook hssfw = new HSSFWorkbook(file); ISheet sheet = hssfw.GetSheetAt(0); System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); IRow headerRow = sheet.GetRow(0); int cellCount = headerRow.LastCellNum; for (int j = 0; j < cellCount; j++) { ICell cell = headerRow.GetCell(j); if (cell == null) { table.Columns.Add(""); } else { table.Columns.Add(cell.ToString()); } } for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); DataRow dataRow = table.NewRow(); for (int j = row.FirstCellNum; j < cellCount; j++) { if (row.GetCell(j) != null) dataRow[j] = row.GetCell(j).ToString(); } table.Rows.Add(dataRow); } dgvShowExcel.DataSource = table; foreach (DataRow dr in table.Rows) { SqlConnection conn = new SqlConnection(); conn.ConnectionString = ConfigurationManager.ConnectionStrings["partialConnectString"].ToString(); SqlCommand cmd = new SqlCommand("Insert Into MCA(m_name,m_type,m_type1,unit,connectString) values (@m_name,@m_type,@m_type1,@unit,@connectString)", conn); cmd.Parameters.Add("@m_name", SqlDbType.VarChar); cmd.Parameters["@m_name"].Value = dr[0].ToString(); cmd.Parameters.Add("@m_type", SqlDbType.VarChar); cmd.Parameters["@m_type"].Value = dr[1].ToString(); cmd.Parameters.Add("@m_type1", SqlDbType.VarChar); cmd.Parameters["@m_type1"].Value = dr[2].ToString(); cmd.Parameters.Add("@unit", SqlDbType.VarChar); cmd.Parameters["@unit"].Value = dr[3].ToString(); cmd.Parameters.Add("@connectString", SqlDbType.VarChar); cmd.Parameters["@connectString"].Value = 0; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); } } MessageBox.Show("录入成功");
解决方案
用double.Parse(你的数据).ToString()转换下。
解决方案二:
编写一个辅助程序转化下吧,没有你要的这种库函数。
时间: 2024-11-03 21:10:10