请教输出DataTable到Excel的文件名是乱码以及输出记录的格式设置
输出Excel按钮事件:
private void OutToExcel_Lk_Click(object sender, System.EventArgs e)
{
string year = Year_DDL.SelectedItem.Value;
string FileName = year + "年年度工作计划.xls ";
ExportDataGrid( "application/ms-excel ",FileName);
}
读取数据库记录到DataTable并输出:
private void ExportDataGrid(string p_FileType, string p_FileName)
{
string sqlStr = "select Convert(int,EveryYearID) as EveryYearID,isnull(Area, ' ') as Area,MapNo,MapName, '1: '+isnull(Scale, ' ') as Scale,SurveyQuality,isnull(SurveyCyc, ' ') as SurveyCyc,isnull(LastYear, ' ') as LastYear,isnull(LastSurveyQuality, ' ') as LastSurveyQuality,isnull(FactArea, ' ') as FactArea,isnull(ConvertArea, ' ') as ConvertArea,isnull(AssistantArea, ' ') as AssistantArea,isnull(TotalArea, ' ') as TotalArea,Case SurveyFlag when '1 ' then '是 ' when '0 ' then '否 ' end as SurveyFlag,isnull(PrintMap, ' ') as PrintMap,isnull(Rmk, ' ') as Rmk from TaskYearMng order by EveryYearID ";
string connectString = ConfigurationSettings.AppSettings[ "DBConnection "];
SqlConnection cn = new SqlConnection( connectString );
SqlDataAdapter da = new SqlDataAdapter( sqlStr,cn );
da.Fill(dsInfo);
DataTable dt = new DataTable();
dt.Columns.Add( "序号 ");
dt.Columns.Add( "区域 ");
dt.Columns.Add( "图号 ");
dt.Columns.Add( "图名 ");
dt.Columns.Add( "比例尺 ");
dt.Columns.Add( "测量性质 ");
dt.Columns.Add( "测量周期(基/检) ");
dt.Columns.Add( "上次测量年份 ");
dt.Columns.Add( "上次测量性质 ");
dt.Columns.Add( "实测面积 ");
dt.Columns.Add( "折算面积 ");
dt.Columns.Add( "辅助面积 ");
dt.Columns.Add( "合计面积 ");
dt.Columns.Add( "多波束测量 ");
dt.Columns.Add( "印图 ");
dt.Columns.Add( "备注 ");
for (int i = 0; i < dsInfo.Tables[0].Rows.Count; i++)
{
DataRow dr = dt.NewRow();
string num = dsInfo.Tables[0].Rows[i][0].ToString();
string id = " ";
if(num.IndexOf( '- ') > -1)
{
string[] nu = num.Split( '- ');
id = nu[0] + "-- " + nu[1];
}
else
{
id = num;
}
dr[0] = id;
dr[1] = dsInfo.Tables[0].Rows[i][1].ToString();
dr[2] = dsInfo.Tables[0].Rows[i][2].ToString();
dr[3] = dsInfo.Tables[0].Rows[i][3].ToString();
dr[4] = dsInfo.Tables[0].Rows[i][4].ToString();
dr[5] = dsInfo.Tables[0].Rows[i][5].ToString();
dr[6] = dsInfo.Tables[0].Rows[i][6].ToString();