我用的是offices 2010
前台是一个fileupload控件,一个button控件,选择excel文件,点击button按钮,直接把excel表中的内容导入到SQL数据库中。请问怎么实现这个功能?详细一些,谢谢!
14 个解决方案
#1
我没做过。我觉得思路是先能读到Excel里的数据,然后对数据进行数据库操作。
#2
string strConn = @"Provider = Microsoft.Ace.OLEDB.12.0; Data Source = " + filePath + "; Extended Properties = 'Excel 12.0;HDR = NO; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
select * from sheetName
OleDbConnection conn = new OleDbConnection(strConn);
select * from sheetName
#3
将Excel里的数据读入DataSet,然后怎么插入数据库中啊
#4
原理:把excel文件上传到指定目录-》上传成功后,程序去操作excel文件(下面代码)-》一行一行把数据对应地添加数据库-提示完成。
代码:读取excel的方法目前两种,一种是引用microsoft的com再操作excel,这个要在服务器安装office,不方便而且还进程死锁的问题。第二种,用NPOI的方法操作excel,只要引用dll文件就可以操作。
资料:http://download.csdn.net/detail/yfs2468/5516955 (我上传的,如果没有下载分私信我,我发你)
代码:读取excel的方法目前两种,一种是引用microsoft的com再操作excel,这个要在服务器安装office,不方便而且还进程死锁的问题。第二种,用NPOI的方法操作excel,只要引用dll文件就可以操作。
资料:http://download.csdn.net/detail/yfs2468/5516955 (我上传的,如果没有下载分私信我,我发你)
#5
首先利用button获取到excel的路径然后读取其内容存到一个datatable里 最后利用它inert into到数据库不就好了 应该很简单
static public DataSet ExcelToDataSet(string filename)//读取
{
try
{
DataSet ds;
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Extended Properties=Excel 8.0;" +
"data source=" + filename;
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT 中药名称,中药分类(植物、动物、矿物和其他),拼音,拼音缩写 FROM [Sheet1$]";
myConn.Open();//打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn); //创建一个 DataSet对象
ds = new DataSet(); //得到自己的DataSet对象
myCommand.Fill(ds);
myConn.Close();
return ds;
}
catch (Exception e)
{
return null;
}
}
static public DataSet ExcelToDataSet(string filename)//读取
{
try
{
DataSet ds;
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Extended Properties=Excel 8.0;" +
"data source=" + filename;
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT 中药名称,中药分类(植物、动物、矿物和其他),拼音,拼音缩写 FROM [Sheet1$]";
myConn.Open();//打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn); //创建一个 DataSet对象
ds = new DataSet(); //得到自己的DataSet对象
myCommand.Fill(ds);
myConn.Close();
return ds;
}
catch (Exception e)
{
return null;
}
}
#6
网速太不给力了,有个图片没有上传上来,只能copy了
private static string exportNPOIExcel(string strFilePath, string strFileName, string strPageName, DataTable dt, string[] strFields, string[] strHeads, bool boolAppend)
{
string strReturn = "success";
//判断Excel是否存在,不存在就创建
checkExcelExists(strFilePath, strFileName, strPageName, strHeads, boolAppend);
try
{
HSSFWorkbook hssfworkbook;
using (FileStream fs = new FileStream(strFilePath + "\\" + strFileName, FileMode.Open))
{
hssfworkbook = new HSSFWorkbook(fs);
fs.Close();
}
Sheet sheet = hssfworkbook.GetSheetAt(0);
int intCurrentRow = sheet.LastRowNum + 1;
for (int i = 0; i < dt.Rows.Count; i++)
{
Row row = sheet.CreateRow(i + intCurrentRow);
for (int j = 0; j < strHeads.Length; j++)
{
Cell cell = row.CreateCell(j);
cell.SetCellValue(dt.Rows[i][strFields[j]].ToString());
}
}
sheet.ForceFormulaRecalculation = true;
using (FileStream fs = new FileStream(strFilePath + "\\" + strFileName, FileMode.Create))
{
hssfworkbook.Write(fs);
fs.Close();
}
}
catch (Exception Ex)
{
LogHelper.Error("导出Excel格式 失败!", Ex);
strReturn = Ex.Message;
}
return strReturn;
}
private static string exportNPOIExcel(string strFilePath, string strFileName, string strPageName, DataTable dt, string[] strFields, string[] strHeads, bool boolAppend)
{
string strReturn = "success";
//判断Excel是否存在,不存在就创建
checkExcelExists(strFilePath, strFileName, strPageName, strHeads, boolAppend);
try
{
HSSFWorkbook hssfworkbook;
using (FileStream fs = new FileStream(strFilePath + "\\" + strFileName, FileMode.Open))
{
hssfworkbook = new HSSFWorkbook(fs);
fs.Close();
}
Sheet sheet = hssfworkbook.GetSheetAt(0);
int intCurrentRow = sheet.LastRowNum + 1;
for (int i = 0; i < dt.Rows.Count; i++)
{
Row row = sheet.CreateRow(i + intCurrentRow);
for (int j = 0; j < strHeads.Length; j++)
{
Cell cell = row.CreateCell(j);
cell.SetCellValue(dt.Rows[i][strFields[j]].ToString());
}
}
sheet.ForceFormulaRecalculation = true;
using (FileStream fs = new FileStream(strFilePath + "\\" + strFileName, FileMode.Create))
{
hssfworkbook.Write(fs);
fs.Close();
}
}
catch (Exception Ex)
{
LogHelper.Error("导出Excel格式 失败!", Ex);
strReturn = Ex.Message;
}
return strReturn;
}
#7
用aspose吧。
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook("c://11.xls");
foreach (Worksheet worksheet in workbook.Worksheets)
{}
#8
利用button获取到excel的路径然后读取其内容存到一个datatable里的代码我有。。。我问的是具体的inert into到数据库的操作,我只能导入一个空表进去,内容没法导进去
#9
protected void Show_Click(object sender, EventArgs e)
{
if ((txtFilePath.HasFile))
{
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
string query = null;
string connString = "";
string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();
//Check file type
if (strFileType == ".xls" || strFileType == ".xlsx")
{
txtFilePath.SaveAs(Server.MapPath("~/UploadedExcel/" + strFileName + strFileType));
}
else
{
return;
}
string strNewPath = Server.MapPath("~/UploadedExcel/" + strFileName + strFileType);
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + "; Extended Properties=\"Excel 8.0;HDR=no;IMEX=1;\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=no;IMEX=1;\"";
}
query = "SELECT * FROM [Sheet1$]";
//Create the connection object
conn = new OleDbConnection(connString);
//Open connection
if (conn.State == ConnectionState.Closed) conn.Open();
//Create the command object
cmd = new OleDbCommand(query, conn);
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
dtgJobs.DataSource = ds.Tables[0];
dtgJobs.DataBind();
//判断是否输入表的名称
string tablename = "";
if (this.tableName.Text == "")
{
tablename = txtFilePath.FileName.ToString().Trim().Substring(0, txtFilePath.FileName.ToString().Trim().LastIndexOf("."));
}
else
{
tablename = this.tableName.Text;
}
/*using (SqlConnection con = new SqlConnection(""))
{
SqlDataAdapter daa = new SqlDataAdapter("", con);
daa.Update(ds);
}*/
//创建新表
CreateTable(tablename, ds);
lblMessage.Text = "Data retrieved successfully! Total Records:" + ds.Tables[0].Rows.Count;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Visible = true;
da.Dispose();
conn.Close();
conn.Dispose();
}
else
{
lblMessage.Text = "Please select an excel file first";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
}
}
这里的creattable怎么写啊
#10
实在不行你就遍历你得到的这个表,一条一条插入呗
#11
protected void Show_Click(object sender, EventArgs e)
{
if ((txtFilePath.HasFile))
{
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
string query = null;
string connString = "";
string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();
//Check file type
if (strFileType == ".xls" || strFileType == ".xlsx")
{
txtFilePath.SaveAs(Server.MapPath("~/UploadedExcel/" + strFileName + strFileType));
}
else
{
return;
}
string strNewPath = Server.MapPath("~/UploadedExcel/" + strFileName + strFileType);
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + "; Extended Properties=\"Excel 8.0;HDR=no;IMEX=1;\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=no;IMEX=1;\"";
}
query = "SELECT * FROM [Sheet1$]";
//Create the connection object
conn = new OleDbConnection(connString);
//Open connection
if (conn.State == ConnectionState.Closed) conn.Open();
//Create the command object
cmd = new OleDbCommand(query, conn);
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
dtgJobs.DataSource = ds.Tables[0];
dtgJobs.DataBind();
//判断是否输入表的名称
string tablename = "";
if (this.tableName.Text == "")
{
tablename = txtFilePath.FileName.ToString().Trim().Substring(0, txtFilePath.FileName.ToString().Trim().LastIndexOf("."));
}
else
{
tablename = this.tableName.Text;
}
/*using (SqlConnection con = new SqlConnection(""))
{
SqlDataAdapter daa = new SqlDataAdapter("", con);
daa.Update(ds);
}*/
//创建新表
CreateTable(tablename, ds);
lblMessage.Text = "Data retrieved successfully! Total Records:" + ds.Tables[0].Rows.Count;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Visible = true;
da.Dispose();
conn.Close();
conn.Dispose();
}
else
{
lblMessage.Text = "Please select an excel file first";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
}
}
这里的creattable怎么写啊
你想复杂了 你不是已经从excel里读取了数据存到ds里了吗,直接把它赋给datatable
DataTable dtExcel = ds.Tables[0];
for (int i = 0; i < dtExcel.Rows.Count; i++)
{
string 信息1= dtExcel.Rows[i]["你的列名"].ToString();
string 信息2= dtExcel.Rows[i]["你的列名"].ToString();
//。。。。。。等等
然后存到数据库里 这个你会把???
INSERT INTO XXXXXX VALUES();就好了
#12
VALUES('" + 信息1+ "','" + 信息2+ "')
#13
可不可以写个完整的程序
#14
同求代码
#1
我没做过。我觉得思路是先能读到Excel里的数据,然后对数据进行数据库操作。
#2
string strConn = @"Provider = Microsoft.Ace.OLEDB.12.0; Data Source = " + filePath + "; Extended Properties = 'Excel 12.0;HDR = NO; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
select * from sheetName
OleDbConnection conn = new OleDbConnection(strConn);
select * from sheetName
#3
将Excel里的数据读入DataSet,然后怎么插入数据库中啊
#4
原理:把excel文件上传到指定目录-》上传成功后,程序去操作excel文件(下面代码)-》一行一行把数据对应地添加数据库-提示完成。
代码:读取excel的方法目前两种,一种是引用microsoft的com再操作excel,这个要在服务器安装office,不方便而且还进程死锁的问题。第二种,用NPOI的方法操作excel,只要引用dll文件就可以操作。
资料:http://download.csdn.net/detail/yfs2468/5516955 (我上传的,如果没有下载分私信我,我发你)
代码:读取excel的方法目前两种,一种是引用microsoft的com再操作excel,这个要在服务器安装office,不方便而且还进程死锁的问题。第二种,用NPOI的方法操作excel,只要引用dll文件就可以操作。
资料:http://download.csdn.net/detail/yfs2468/5516955 (我上传的,如果没有下载分私信我,我发你)
#5
首先利用button获取到excel的路径然后读取其内容存到一个datatable里 最后利用它inert into到数据库不就好了 应该很简单
static public DataSet ExcelToDataSet(string filename)//读取
{
try
{
DataSet ds;
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Extended Properties=Excel 8.0;" +
"data source=" + filename;
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT 中药名称,中药分类(植物、动物、矿物和其他),拼音,拼音缩写 FROM [Sheet1$]";
myConn.Open();//打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn); //创建一个 DataSet对象
ds = new DataSet(); //得到自己的DataSet对象
myCommand.Fill(ds);
myConn.Close();
return ds;
}
catch (Exception e)
{
return null;
}
}
static public DataSet ExcelToDataSet(string filename)//读取
{
try
{
DataSet ds;
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Extended Properties=Excel 8.0;" +
"data source=" + filename;
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT 中药名称,中药分类(植物、动物、矿物和其他),拼音,拼音缩写 FROM [Sheet1$]";
myConn.Open();//打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn); //创建一个 DataSet对象
ds = new DataSet(); //得到自己的DataSet对象
myCommand.Fill(ds);
myConn.Close();
return ds;
}
catch (Exception e)
{
return null;
}
}
将Excel里的数据读入DataSet,然后怎么插入数据库中啊
将Excel里的数据读入DataSet,然后怎么插入数据库中啊
#6
网速太不给力了,有个图片没有上传上来,只能copy了
private static string exportNPOIExcel(string strFilePath, string strFileName, string strPageName, DataTable dt, string[] strFields, string[] strHeads, bool boolAppend)
{
string strReturn = "success";
//判断Excel是否存在,不存在就创建
checkExcelExists(strFilePath, strFileName, strPageName, strHeads, boolAppend);
try
{
HSSFWorkbook hssfworkbook;
using (FileStream fs = new FileStream(strFilePath + "\\" + strFileName, FileMode.Open))
{
hssfworkbook = new HSSFWorkbook(fs);
fs.Close();
}
Sheet sheet = hssfworkbook.GetSheetAt(0);
int intCurrentRow = sheet.LastRowNum + 1;
for (int i = 0; i < dt.Rows.Count; i++)
{
Row row = sheet.CreateRow(i + intCurrentRow);
for (int j = 0; j < strHeads.Length; j++)
{
Cell cell = row.CreateCell(j);
cell.SetCellValue(dt.Rows[i][strFields[j]].ToString());
}
}
sheet.ForceFormulaRecalculation = true;
using (FileStream fs = new FileStream(strFilePath + "\\" + strFileName, FileMode.Create))
{
hssfworkbook.Write(fs);
fs.Close();
}
}
catch (Exception Ex)
{
LogHelper.Error("导出Excel格式 失败!", Ex);
strReturn = Ex.Message;
}
return strReturn;
}
private static string exportNPOIExcel(string strFilePath, string strFileName, string strPageName, DataTable dt, string[] strFields, string[] strHeads, bool boolAppend)
{
string strReturn = "success";
//判断Excel是否存在,不存在就创建
checkExcelExists(strFilePath, strFileName, strPageName, strHeads, boolAppend);
try
{
HSSFWorkbook hssfworkbook;
using (FileStream fs = new FileStream(strFilePath + "\\" + strFileName, FileMode.Open))
{
hssfworkbook = new HSSFWorkbook(fs);
fs.Close();
}
Sheet sheet = hssfworkbook.GetSheetAt(0);
int intCurrentRow = sheet.LastRowNum + 1;
for (int i = 0; i < dt.Rows.Count; i++)
{
Row row = sheet.CreateRow(i + intCurrentRow);
for (int j = 0; j < strHeads.Length; j++)
{
Cell cell = row.CreateCell(j);
cell.SetCellValue(dt.Rows[i][strFields[j]].ToString());
}
}
sheet.ForceFormulaRecalculation = true;
using (FileStream fs = new FileStream(strFilePath + "\\" + strFileName, FileMode.Create))
{
hssfworkbook.Write(fs);
fs.Close();
}
}
catch (Exception Ex)
{
LogHelper.Error("导出Excel格式 失败!", Ex);
strReturn = Ex.Message;
}
return strReturn;
}
#7
用aspose吧。
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook("c://11.xls");
foreach (Worksheet worksheet in workbook.Worksheets)
{}
#8
首先利用button获取到excel的路径然后读取其内容存到一个datatable里 最后利用它inert into到数据库不就好了 应该很简单
static public DataSet ExcelToDataSet(string filename)//读取
{
try
{
DataSet ds;
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Extended Properties=Excel 8.0;" +
"data source=" + filename;
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT 中药名称,中药分类(植物、动物、矿物和其他),拼音,拼音缩写 FROM [Sheet1$]";
myConn.Open();//打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn); //创建一个 DataSet对象
ds = new DataSet(); //得到自己的DataSet对象
myCommand.Fill(ds);
myConn.Close();
return ds;
}
catch (Exception e)
{
return null;
}
}
将Excel里的数据读入DataSet,然后怎么插入数据库中啊 将Excel里的数据读入DataSet,然后怎么插入数据库中啊
利用button获取到excel的路径然后读取其内容存到一个datatable里的代码我有。。。我问的是具体的inert into到数据库的操作,我只能导入一个空表进去,内容没法导进去
#9
protected void Show_Click(object sender, EventArgs e)
{
if ((txtFilePath.HasFile))
{
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
string query = null;
string connString = "";
string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();
//Check file type
if (strFileType == ".xls" || strFileType == ".xlsx")
{
txtFilePath.SaveAs(Server.MapPath("~/UploadedExcel/" + strFileName + strFileType));
}
else
{
return;
}
string strNewPath = Server.MapPath("~/UploadedExcel/" + strFileName + strFileType);
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + "; Extended Properties=\"Excel 8.0;HDR=no;IMEX=1;\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=no;IMEX=1;\"";
}
query = "SELECT * FROM [Sheet1$]";
//Create the connection object
conn = new OleDbConnection(connString);
//Open connection
if (conn.State == ConnectionState.Closed) conn.Open();
//Create the command object
cmd = new OleDbCommand(query, conn);
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
dtgJobs.DataSource = ds.Tables[0];
dtgJobs.DataBind();
//判断是否输入表的名称
string tablename = "";
if (this.tableName.Text == "")
{
tablename = txtFilePath.FileName.ToString().Trim().Substring(0, txtFilePath.FileName.ToString().Trim().LastIndexOf("."));
}
else
{
tablename = this.tableName.Text;
}
/*using (SqlConnection con = new SqlConnection(""))
{
SqlDataAdapter daa = new SqlDataAdapter("", con);
daa.Update(ds);
}*/
//创建新表
CreateTable(tablename, ds);
lblMessage.Text = "Data retrieved successfully! Total Records:" + ds.Tables[0].Rows.Count;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Visible = true;
da.Dispose();
conn.Close();
conn.Dispose();
}
else
{
lblMessage.Text = "Please select an excel file first";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
}
}
这里的creattable怎么写啊
#10
实在不行你就遍历你得到的这个表,一条一条插入呗
#11
protected void Show_Click(object sender, EventArgs e)
{
if ((txtFilePath.HasFile))
{
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
string query = null;
string connString = "";
string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();
//Check file type
if (strFileType == ".xls" || strFileType == ".xlsx")
{
txtFilePath.SaveAs(Server.MapPath("~/UploadedExcel/" + strFileName + strFileType));
}
else
{
return;
}
string strNewPath = Server.MapPath("~/UploadedExcel/" + strFileName + strFileType);
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + "; Extended Properties=\"Excel 8.0;HDR=no;IMEX=1;\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=no;IMEX=1;\"";
}
query = "SELECT * FROM [Sheet1$]";
//Create the connection object
conn = new OleDbConnection(connString);
//Open connection
if (conn.State == ConnectionState.Closed) conn.Open();
//Create the command object
cmd = new OleDbCommand(query, conn);
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
dtgJobs.DataSource = ds.Tables[0];
dtgJobs.DataBind();
//判断是否输入表的名称
string tablename = "";
if (this.tableName.Text == "")
{
tablename = txtFilePath.FileName.ToString().Trim().Substring(0, txtFilePath.FileName.ToString().Trim().LastIndexOf("."));
}
else
{
tablename = this.tableName.Text;
}
/*using (SqlConnection con = new SqlConnection(""))
{
SqlDataAdapter daa = new SqlDataAdapter("", con);
daa.Update(ds);
}*/
//创建新表
CreateTable(tablename, ds);
lblMessage.Text = "Data retrieved successfully! Total Records:" + ds.Tables[0].Rows.Count;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Visible = true;
da.Dispose();
conn.Close();
conn.Dispose();
}
else
{
lblMessage.Text = "Please select an excel file first";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
}
}
这里的creattable怎么写啊
你想复杂了 你不是已经从excel里读取了数据存到ds里了吗,直接把它赋给datatable
DataTable dtExcel = ds.Tables[0];
for (int i = 0; i < dtExcel.Rows.Count; i++)
{
string 信息1= dtExcel.Rows[i]["你的列名"].ToString();
string 信息2= dtExcel.Rows[i]["你的列名"].ToString();
//。。。。。。等等
然后存到数据库里 这个你会把???
INSERT INTO XXXXXX VALUES();就好了
#12
VALUES('" + 信息1+ "','" + 信息2+ "')
#13
可不可以写个完整的程序
#14
同求代码