-

- 加为好友
- 发送私信
- 在线聊天
|
| 发表于:2008-08-21 10:05:562楼 得分:5 |
using (OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strDestPath + ";Extended Properties=\"Excel 8.0;HDR=YES;\"")) { con.Open(); string strCity = "", strCmd = ""; OleDbCommand cmd = new OleDbCommand(); cmd.Connection = con; while ((strLine = sr.ReadLine()) != null) { strLine = Regex.Replace(strLine, "\"", ""); try { string[] AstrL = strLine.Split(','); if (strCity != AstrL[0]) { strCity = AstrL[0]; strCmd = "create table " + strCity + " ("; foreach (string str1 in Astr) strCmd += str1 + " char(255),"; strCmd = strCmd.TrimEnd(new char[] { ',' }); strCmd += ")"; //MessageBox.Show(strCmd); break; cmd.CommandText = strCmd; cmd.ExecuteNonQuery(); strCmd = "insert into " + strCity + " values("; foreach (string str1 in AstrL) strCmd += "'" + str1 + "',"; strCmd = strCmd.TrimEnd(new char[] { ',' }); strCmd += ")"; cmd.CommandText = strCmd; cmd.ExecuteNonQuery(); } else { strCmd = "insert into " + strCity + " values("; foreach (string str1 in AstrL) strCmd += "'" + str1 + "',"; strCmd = strCmd.TrimEnd(new char[] { ',' }); strCmd += ")"; cmd.CommandText = strCmd; cmd.ExecuteNonQuery(); } } catch (Exception ex) { MessageBox.Show(strCmd + ex.Message); break; } } } 上面是一个把文本文件导出到excel的多个worksheet的例子,你可以参考以下。datagridview也可以用这种方式。 | | |
修改
删除
举报
引用
回复
| |