public void ExportDataToCVS(String fileName, System.Data.DataTable dt) { if (MyStringUtil.isEmpty(fileName)) { return; } if (dt == null || dt.Rows.Count <= 0) { return; } StreamWriter sw = new StreamWriter(fileName, false, Encoding.GetEncoding("gb2312")); StringBuilder sb = new StringBuilder(); for (int i = 0; i < dt.Columns.Count; i++) { sb.Append(dt.Columns[i].ColumnName.ToString() + ","); } sb.Append(Environment.NewLine); for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { sb.Append(dt.Rows[i][j].ToString() + ","); } sb.Append(Environment.NewLine);//每写一行数据后换行 } sw.Write(sb.ToString()); sw.Flush(); sw.Close();//释放资源 }
public static DataSet ExcelToDS(String sheetName, String condition) { DataSet ds = null; OleDbConnection conn = null; try { conn = new OleDbConnection(strConn); conn.Open(); string strExcel = ""; OleDbDataAdapter myCommand = null; strExcel = "select * from [" + sheetName + "$]"; if (!MyStringUtil.isEmpty(condition)) { strExcel = strExcel + " where " + condition; } myCommand = new OleDbDataAdapter(strExcel, strConn); ds = new DataSet(); myCommand.Fill(ds, "table1"); conn.Close(); } catch (Exception e) { Log.Write(e.Message); } finally { if (conn != null && ConnectionState.Open == conn.State) { conn.Close(); } } return(ds); }
/// <summary> /// 获取某个数据表的列名及中文名 /// </summary> /// <param name="tableName"></param> /// <returns></returns> public Dictionary <String, String> getColumns(string tableName) { Dictionary <String, String> result = new Dictionary <String, String>(); string queryString = "select * from ts_table_label where TABLE_NAME='" + tableName + "'"; DataTable dataTable = ExecuteSelect(queryString); foreach (DataRow mDr in dataTable.Rows) { string columnName = (mDr["COL_NAME"].Equals(DBNull.Value)) ? "" : (String)mDr["COL_NAME"]; string columnLabel = (mDr["COL_LABEL"].Equals(DBNull.Value)) ? "" : (String)mDr["COL_LABEL"]; if (!MyStringUtil.isEmpty(columnName)) { if (MyStringUtil.isEmpty(columnLabel)) { columnLabel = columnName; } result.Add(columnName, columnLabel); } } return(result); }