无需控件直接导出xls(csv)
/// <summary>
/// 执行导出
/// </summary>
/// <param name="ds">要导出的DataSet</param>
/// <param name="strExcelFileName">要导出的文件名</param>
private void dataTableToCsv(DataTable table, string file)
{
string title = "";
Dictionary<string ,string> xlsDic=new Dictionary<string,string>();
xlsDic.Add("F_20", "实出勤天数");
xlsDic.Add("F_31", "应出勤天数");
xlsDic.Add("F_22", "病假天数");
xlsDic.Add("F_24", "事假天数");
xlsDic.Add("F_25", "旷工天数");
xlsDic.Add("F_37", "月发绩效");
xlsDic.Add("F_38", "出差补助");
xlsDic.Add("F_39", "其他补助");
xlsDic.Add("F_40", "调补工资");
xlsDic.Add("F_41", "餐补");
xlsDic.Add("F_27", "加班费");
xlsDic.Add("F_34", "上月病假是否满月");
xlsDic.Add("F_36", "工资标准");
xlsDic.Add("cGZGradeNum", "cGZGradeNum");
xlsDic.Add("iPsnGrd_id", "iPsnGrd_id");
xlsDic.Add("cPsn_Num", "编号");
xlsDic.Add("cPsn_Name", "姓名");
xlsDic.Add("cDept_Num", "部门编号");
xlsDic.Add("iYear", "年");
xlsDic.Add("iMonth", "月");
FileStream fs = new FileStream(file, FileMode.OpenOrCreate);
//FileStream fs1 = File.Open(file, FileMode.Open, FileAccess.Read);
StreamWriter sw = new StreamWriter(new BufferedStream(fs), System.Text.Encoding.Default);
for (int i = 0; i < table.Columns.Count; i++)
{
title += xlsDic[table.Columns[i].ColumnName] + "\t"; //栏位:自动跳到下一单元格
}
title = title.Substring(0, title.Length - 1) + "\n";
sw.Write(title);
foreach (DataRow row in table.Rows)
{
string line = "";
for (int i = 0; i < table.Columns.Count; i++)
{
switch (i)
{
case 0:
case 1:
case 2:
case 3:
case 4:
line += row[i].ToString().Trim() + "\t"; //内容:自动跳到下一单元格
break;
case 5:
line += DateTime.Now.Year.ToString() + "\t";
break;
case 6:
line += DateTime.Now.Month.ToString() + "\t";
break;
default:
line += "\t"; //内容为空:自动跳到下一单元格
break;
}
}
line = line.Substring(0, line.Length - 1) + "\n";
sw.Write(line);
}
sw.Close();
fs.Close();
}