我用一个函数来生成xls。但生成的xls工作表没有带箭头键的滚动功能

时间:2015-04-14 08:31:45

标签: excel attributes schema spreadsheet

您好我使用以下函数生成一个excel工作簿模板,我后来用它来生成一个excel xls文件并将其写入响应(asp.net)。

private static string getWorkbookTemplate()
{
    var 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=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#000000""/>{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"" ss:Name=""Header"">{0}", Environment.NewLine);
    sb.AppendFormat(@"   <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#FFFFFF""{0}", Environment.NewLine);
    sb.AppendFormat(@"    ss:Bold=""1""/>{0}", Environment.NewLine);
    sb.AppendFormat(@"   <Interior ss:Color=""#5A0B0E""  ss:Pattern=""Solid""/>{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(@"  <Style ss:ID=""s64"" ss:Name=""RowArea"">{0}", Environment.NewLine);
    sb.AppendFormat(@"   <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#FFFFFF""{0}", Environment.NewLine);
    sb.AppendFormat(@"    ss:Bold=""1""/>{0}", Environment.NewLine);
    sb.AppendFormat(@"   <Interior ss:Color=""#723B3E""  ss:Pattern=""Solid""/>{0}", Environment.NewLine);
    sb.AppendFormat(@"  </Style>{0}", Environment.NewLine);
    sb.AppendFormat(@"  <Style ss:ID=""s65"" ss:Name=""SummarySection"">{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=""0""/>{0}", Environment.NewLine);//
    sb.AppendFormat(@"   <Interior ss:Color=""#FFE5E6""  ss:Pattern=""Solid""/>{0}", Environment.NewLine);
    sb.AppendFormat(@"  </Style>{0}", Environment.NewLine);
    sb.AppendFormat(@"  <Style ss:ID=""s66"" ss:Name=""TotalRow"">{0}", Environment.NewLine);
    sb.AppendFormat(@"   <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11""{0}", Environment.NewLine);
    sb.AppendFormat(@"    ss:Bold=""1""/>{0}", Environment.NewLine);
    sb.AppendFormat(@"   <Interior ss:Color=""#FFE5E6""  ss:Pattern=""Solid""/>{0}", Environment.NewLine);
    sb.AppendFormat(@"  </Style>{0}", Environment.NewLine);
    sb.AppendFormat(@"  <Style ss:ID=""s67"" ss:Name=""PosValues"">{0}", Environment.NewLine);
    sb.AppendFormat(@"   <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#008000""/>{0}", Environment.NewLine);
    // sb.AppendFormat(@"    ss:Bold=""1""/>{0}", Environment.NewLine);//
    sb.AppendFormat(@"   <Interior ss:Color=""#FFE5E6""  ss:Pattern=""Solid""/>{0}", Environment.NewLine);
    sb.AppendFormat(@"  </Style>{0}", Environment.NewLine);
    sb.AppendFormat(@"  <Style ss:ID=""s68"" ss:Name=""NegValues"">{0}", Environment.NewLine);
    sb.AppendFormat(@"   <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#A31515""/>{0}", Environment.NewLine);
    //  sb.AppendFormat(@"    ss:Bold=""1""/>{0}", Environment.NewLine);//
    sb.AppendFormat(@"   <Interior ss:Color=""#FFE5E6""  ss:Pattern=""Solid""/>{0}", Environment.NewLine);
    sb.AppendFormat(@"  </Style>{0}", Environment.NewLine);

    //New Style Defined Ganesh
    sb.AppendFormat(@"  <Style ss:ID=""s69"" ss:Name=""TotalRowSummarySection"">{0}", Environment.NewLine);
    sb.AppendFormat(@"   <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#000000"" ss:Bold=""1""/>{0}", Environment.NewLine);
    //sb.AppendFormat(@"    />{0}", Environment.NewLine);//
    sb.AppendFormat(@"   <Interior ss:Color=""#FFE5E6""  ss:Pattern=""Solid""/>{0}", Environment.NewLine);


    sb.AppendFormat(@"  </Style>{0}", Environment.NewLine);
    sb.AppendFormat(@"  <Style ss:ID=""s70"" ss:Name=""BoldPosValues"">{0}", Environment.NewLine);
    sb.AppendFormat(@"   <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#008000"" ss:Bold=""1""/>{0}", Environment.NewLine);
    //sb.AppendFormat(@"    />{0}", Environment.NewLine);
    sb.AppendFormat(@"   <Interior ss:Color=""#FFE5E6""  ss:Pattern=""Solid""/>{0}", Environment.NewLine);
    sb.AppendFormat(@"  </Style>{0}", Environment.NewLine);

    sb.AppendFormat(@"  <Style ss:ID=""s71"" ss:Name=""BoldNegValues"">{0}", Environment.NewLine);
    sb.AppendFormat(@"   <Font ss:FontName=""Calibri"" x:Family=""Swiss"" ss:Size=""11"" ss:Color=""#A31515"" ss:Bold=""1""/>{0}", Environment.NewLine);
    //sb.AppendFormat(@"    />{0}", Environment.NewLine);
    sb.AppendFormat(@"   <Interior ss:Color=""#FFE5E6""  ss:Pattern=""Solid""/>{0}", Environment.NewLine);
    sb.AppendFormat(@"  </Style>{0}", Environment.NewLine);
    //New Style Ends

    sb.AppendFormat(@" </Styles>{0}", Environment.NewLine);
    sb.Append(@"{0}\r\n</Workbook>");
    return sb.ToString();
}

我能够成功生成文件,也可以在excel中打开文件。但是当我使用箭头键通过单元格时,纸张仍然停滞不前。即它不会滚动,当我使用鼠标并按下滚动条上的滚动按钮时,它可以工作。除了excel 2007中的公式栏之外,光标一直保持到我可以看到其位置的新单元格。但页面不会滚动。我使用EPPlus创建了另一个excel文件,但没有这个问题。任何人都可以建议是否可以控制Excel电子表格架构中的滚动。

1 个答案:

答案 0 :(得分:0)

我发现这个文件实际上存储为电子表格ML(xml)文件。这就是我得到这个问题的原因。如果我将其保存为2003 xls,那么我可以使用箭头键滚动。并且我在堆栈溢出时找到了答案,其中建议为了实现这一点,我需要将其发送到可以访问excel并且可以生成所需的excel文件的服务。我尝试使用WCF,但下载速度超级慢得令人无法接受。所以我使用了这种技术。