2010-07-08 2 views
0

엑셀로 내보내는 데이터 세트가 Excel로 내보내지면 시트의 숫자가 텍스트로 표시됩니다. 여기 엑셀 데이터 세트를 내보낼 때 숫자가 텍스트로 변환됩니다.

코드입니다 :

public class Excel 
{ 
    const int rowLimit = 65000; 

    private static string getWorkbookTemplate() 
    { 
     StringBuilder sb = new StringBuilder(818); 
     sb.AppendFormat(@"<?xml version=""1.0""?>{0}", Environment.NewLine); 
     sb.AppendFormat(@"<?mso-application progid=""Excel.Sheet""?>{0}", Environment.NewLine); 
     sb.AppendFormat(@"<Workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet""{0}", Environment.NewLine); 
     sb.AppendFormat(@" xmlns:o=""urn:schemas-microsoft-com:office:office""{0}", Environment.NewLine); 
     sb.AppendFormat(@" xmlns:x=""urn:schemas-microsoft-com:office:excel""{0}", Environment.NewLine); 
     sb.AppendFormat(@" xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet""{0}", Environment.NewLine); 
     sb.AppendFormat(@" xmlns:html=""http://www.w3.org/TR/REC-html40"">{0}", Environment.NewLine); 
     sb.AppendFormat(@" <Styles>{0}", Environment.NewLine); 
     sb.AppendFormat(@" <Style ss:ID=""Default"" ss:Name=""Normal"">{0}", Environment.NewLine); 
     sb.AppendFormat(@" <Alignment ss:Vertical=""Bottom""/>{0}", Environment.NewLine); 
     sb.AppendFormat(@" <Borders/>{0}", Environment.NewLine); 
     sb.AppendFormat(@" <Font ss:FontName=""Verdana"" x:Family=""Swiss"" ss:Size=""12"" ss:Color=""#0000A0""/>{0}", Environment.NewLine); 
     sb.AppendFormat(@" <Interior/>{0}", Environment.NewLine); 
     sb.AppendFormat(@" <NumberFormat/>{0}", Environment.NewLine); 
     sb.AppendFormat(@" <Protection/>{0}", Environment.NewLine); 
     sb.AppendFormat(@" </Style>{0}", Environment.NewLine); 
     sb.AppendFormat(@" <Style ss:ID=""s62"">{0}", Environment.NewLine); 
     sb.AppendFormat(@" <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#000000""{0}", Environment.NewLine); 
     sb.AppendFormat(@" ss:Bold=""1""/>{0}", Environment.NewLine); 
     sb.AppendFormat(@" </Style>{0}", Environment.NewLine); 
     sb.AppendFormat(@" <Style ss:ID=""s63"">{0}", Environment.NewLine); 
     sb.AppendFormat(@" <NumberFormat ss:Format=""Short Date""/>{0}", Environment.NewLine); 
     sb.AppendFormat(@" </Style>{0}", Environment.NewLine); 
     sb.AppendFormat(@" </Styles>{0}", Environment.NewLine); 
     sb.Append(@"{0}\r\n</Workbook>"); 
     return sb.ToString(); 
    } 

    private static string replaceXmlChar(string input) 
    { 
     input = input.Replace("&", "&amp"); 
     input = input.Replace("<", "&lt;"); 
     input = input.Replace(">", "&gt;"); 
     input = input.Replace("\"", "&quot;"); 
     input = input.Replace("'", "&apos;"); 
     return input; 
    } 

    private static string getCell(Type type, object cellData) 
    { 
     Object data = (cellData is DBNull) ? "" : cellData; 
     if (type.Name.Contains("Int") || type.Name.Contains("Double") || type.Name.Contains("Decimal")) return string.Format("<Cell><Data ss:Type=\"Number\">{0}</Data></Cell>", data); 
     if (type.Name.Contains("Date") && data.ToString() != string.Empty) 
     { 
      return string.Format("<Cell ss:StyleID=\"s63\"><Data ss:Type=\"DateTime\">{0}</Data></Cell>", Convert.ToDateTime(data).ToString("yyyy-MM-dd")); 
     } 
     return string.Format("<Cell><Data ss:Type=\"String\">{0}</Data></Cell>", replaceXmlChar(data.ToString())); 
    } 
    private static string getWorksheets(DataSet source) 
    { 
     StringWriter sw = new StringWriter(); 
     if (source == null || source.Tables.Count == 0) 
     { 
      sw.Write("<Worksheet ss:Name=\"Sheet1\">\r\n<Table>\r\n<Row><Cell><Data ss:Type=\"String\"></Data></Cell></Row>\r\n</Table>\r\n</Worksheet>"); 
      return sw.ToString(); 
     } 
     foreach (DataTable dt in source.Tables) 
     { 
      if (dt.Rows.Count == 0) 
       sw.Write("<Worksheet ss:Name=\"" + replaceXmlChar(dt.TableName) + "\">\r\n<Table>\r\n<Row><Cell ss:StyleID=\"s62\"><Data ss:Type=\"String\"></Data></Cell></Row>\r\n</Table>\r\n</Worksheet>"); 
      else 
      { 
       //write each row data     
       int sheetCount = 0; 
       for (int i = 0; i < dt.Rows.Count; i++) 
       { 
        if ((i % rowLimit) == 0) 
        { 
         //add close tags for previous sheet of the same data table 
         if ((i/rowLimit) > sheetCount) 
         { 
          sw.Write("\r\n</Table>\r\n</Worksheet>"); 
          sheetCount = (i/rowLimit); 
         } 
         sw.Write("\r\n<Worksheet ss:Name=\"" + replaceXmlChar(dt.TableName) + 
           (((i/rowLimit) == 0) ? "" : Convert.ToString(i/rowLimit)) + "\">\r\n<Table>"); 
         //write column name row 
         sw.Write("\r\n<Row>"); 
         foreach (DataColumn dc in dt.Columns) 
          sw.Write(string.Format("<Cell ss:StyleID=\"s62\"><Data ss:Type=\"String\">{0}</Data></Cell>", replaceXmlChar(dc.ColumnName))); 
         sw.Write("</Row>"); 
        } 
        sw.Write("\r\n<Row>"); 
        foreach (DataColumn dc in dt.Columns) 
         sw.Write(getCell(dc.DataType, dt.Rows[i][dc.ColumnName])); 
        sw.Write("</Row>"); 
       } 
       sw.Write("\r\n</Table>\r\n</Worksheet>"); 
      } 
     } 

     return sw.ToString(); 
    } 
    public static string GetExcelXml(DataTable dtInput, string filename) 
    { 
     string excelTemplate = getWorkbookTemplate(); 
     DataSet ds = new DataSet(); 
     ds.Tables.Add(dtInput.Copy()); 
     string worksheets = getWorksheets(ds); 
     string excelXml = string.Format(excelTemplate, worksheets); 
     return excelXml; 
    } 

    public static string GetExcelXml(DataSet dsInput, string filename) 
    { 
     string excelTemplate = getWorkbookTemplate(); 
     string worksheets = getWorksheets(dsInput); 
     string excelXml = string.Format(excelTemplate, worksheets); 
     return excelXml; 
    } 

    public static void ToExcel(DataSet dsInput, string filename, HttpResponse response) 
    { 
     string excelXml = GetExcelXml(dsInput, filename); 
     response.Clear(); 
     response.AppendHeader("Content-Type", "application/vnd.ms-excel"); 
     response.AppendHeader("Content-disposition", "attachment; filename=" + filename); 
     response.Write(excelXml); 
     response.Flush(); 
     response.End(); 
    } 

    public static void ToExcel(DataTable dtInput, string filename, HttpResponse response) 
    { 
     DataSet ds = new DataSet(); 
     ds.Tables.Add(dtInput.Copy()); 
     ToExcel(ds, filename, response); 
    } 
} 
+0

getCell 함수에서 if 문이 숫자 값과 일치하는 것은 무엇입니까? 이 오픈 오피스에서 제대로 열리지 않습니다 –

+0

(일) 3.0 –

답변

0

나는 이유를 확실히 모르겠지만, 왠지 다시 우리의 프로젝트 오랜 시간에 비슷한 취급 기억한다. 수정 (또는 해결 방법)은 숫자 열의 숫자 앞에 작은 따옴표 (')를 붙이는 것입니다.

+0

Madhur 이봐, 어디 또는 데이터베이스 측 뒤에 코드에 메신저 혼란 당신은 내가 어디에 코드에서 확실하지 않다 – SmartDev

+0

명확 감사하십시오 수해야 이것 이상으로 할 수 있습니다. 그러나 내가해야 할 일이 무엇인지 말해 줄 수 있습니다 - 그렇게하면 어디서 할 수 있는지 알아낼 수 있습니다. 스프레드 시트/통합 문서의 "셀"에 "숫자"/ "숫자"를 쓰는 코드가 있습니다. 10 "을"10 "으로 작성하는 대신"10 "으로 작성하면 속임수를 쓸 수 있습니다. Excel에서 이러한 셀을 볼 때"10 "은 수 – madhurtanwani

관련 문제