Excel导入问题
EXCEL导出的时候,我是把EXCEL当成一张表来处理的,但是导出的时候全部是文本形式了,如果是数字的就不转换玮文本,请问,如何处理public static void Dt_to_Excel(DataTable Table, string DefFileName)
{
System.Data.OleDb.OleDbConnection MyOleDbCn = new OleDbConnection();
System.Data.OleDb.OleDbCommand MyOleDbCmd = new System.Data.OleDb.OleDbCommand();
DataTable MyTable = new DataTable();
int intRowsCnt, intColsCnt;
string strSql, strFlName;
Object Fso = new System.Object();
if (Table == null)
{
MessageBox.Show("未取得数据,无法导出", "导出错误 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
MyTable = Table;
if (MyTable.Rows.Count - 1 < 0)
{
MessageBox.Show("未取得数据,无法导出", "导出错误 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string FileName = " ";
SaveFileDialog SaveFileDialog = new SaveFileDialog();
SaveFileDialog.Title = "保存为 ";
SaveFileDialog.Filter = ".xls |*.xls ";
SaveFileDialog.FileName = DefFileName;
if (SaveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FileName = SaveFileDialog.FileName.Trim();
}
if (FileName.Trim() == " ")
{
return;
}
strFlName = FileName.Trim();
if (System.IO.File.Exists(FileName) == true)
{
Microsoft.VisualBasic.FileSystem.Kill(FileName);
}
try
{
MyOleDbCn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; "
+ "Data Source= " + strFlName + ";"
+ "Extended ProPerties= " + " 'Excel 8.0;HDR=Yes; " + "'";
MyOleDbCn.Open();
MyOleDbCmd.Connection = MyOleDbCn;
MyOleDbCmd.CommandType = CommandType.Text;
strSql = "CREATE TABLE " + DefFileName + "( ";
for (intColsCnt = 0; intColsCnt <= MyTable.Columns.Count - 1; intColsCnt++)
{
if (intColsCnt != MyTable.Columns.Count - 1)
{
strSql = strSql + ChangeChar(MyTable.Columns[intColsCnt].Caption) + " text, ";
}
else
{
strSql = strSql + ChangeChar(MyTable.Columns[intColsCnt].Caption) + " text) ";
}
}
MyOleDbCmd.CommandText = strSql;
MyOleDbCmd.ExecuteNonQuery();
// '插入各行
for (intRowsCnt = 0; intRowsCnt <= MyTable.Rows.Count - 1; intRowsCnt++)
{
strSql = "INSERT INTO " + DefFileName + " VALUES( ' ";
for (intColsCnt = 0; intColsCnt <= MyTable.Columns.Count - 1; intColsCnt++)
{
if (intColsCnt != MyTable.Columns.Count - 1)
{
strSql = strSql + ChangeChar(MyTable.Rows[intRowsCnt][intColsCnt]) + " ', ' ";
}
else
{
strSql = strSql + ChangeChar(MyTable.Rows[intRowsCnt][intColsCnt]) + " ') ";
}
}
MyOleDbCmd.CommandText = strSql;
MyOleDbCmd.ExecuteNonQuery();
}
MessageBox.Show("数据已经成功导入EXCEL文件 " + strFlName, "数据导出 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
MyOleDbCmd.Dispose();
MyOleDbCn.Close();
MyOleDbCn.Dispose();
}
}
private static string ChangeChar(object Sqlchar)
{
if (Convert.IsDBNull(Sqlchar) == true)
{
return " ";
}
string tStr = " ";
tStr = Sqlchar.ToString().Trim().Replace(" ' ", " ' ' ");
tStr = tStr.Replace(" ¦ ", "_ ");
//ChangeChar = tStr;
return tStr;
} --------------------编程问答-------------------- 建议不要用循环来进行数据的导出Excel操作,数据量少的时候还行,当数据量大的时候很浪费时间,建议你用excel自己的QueryTable,,通过传递一个sql语句给excel,让excel自己生成相应的数据,这样是比较合理的,对于动态生成数据效果也是相当好的
补充:.NET技术 , C#