Popular Posts
Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i... LogonUser Function : impersonate a windows user // This sample demonstrates the use of the WindowsIdentity class to impersonate a user. // IMPORTANT NOTES:  // This sample can be run only ...
Blog Archive
Stats
Export excel using html format cause leading zero disappeared
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication4
{
    class Program
    {
        static char[] vs = "qazxswedcvfrtgb nhyujmkioplPLOKIJU HYGTFRDESWSQAZXCVBNM ".ToCharArray();
 
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder(@"<html xmlns:o=""urn:schemas-microsoft-com:office:office""
xmlns:x=""urn:schemas-microsoft-com:office:excel""
xmlns=""http://www.w3.org/TR/REC-html40"">
<head>
</head><table border=""1""><thead><tr><th>column A</th><th>column B</th><th>column C</th><th>column D</th></tr></thead><tbody>");
            Random r = new Random();
            for (int i = 0; i < 10; i++)
            {
                    sb.AppendFormat(
                        @"<tr><td>{0}</td><td>{1}</td><td x:str=""{2:0000000000}"">{2:0000000000}</td><td>{3}</td></tr>",
                        getRandomWord(r),
                        r.NextDouble(),
                        r.Next(10000000),
                        "test"
                    ).AppendLine();
            }
            sb.Append("</tbody></body></html>");
 
            System.IO.File.WriteAllText("z:/test.xls", sb.ToString());
        }
 
        static string getRandomWord(Random r)
        {
            int length = r.Next(5, 15);
            StringBuilder sb = new StringBuilder();
            while (length-- > 0)
            {
                sb.Append(vs[r.Next(vs.Length - 1)]);
            }
            return sb.ToString();
        }
    }
}
(export without x:str attribute)
(export with x:str attribute)

Use xml format example

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:o="urn:schemas-microsoft-com:office:office"
          xmlns:x="urn:schemas-microsoft-com:office:excel"
          xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:html="http://www.w3.org/TR/REC-html40">
  <Worksheet ss:Name="Sheet1">
    <Table  ss:DefaultColumnWidth="54" ss:DefaultRowHeight="16.5">
      <Row>
        <Cell>
          <Data ss:Type="String">KPmBYSAYyugQe</Data>
        </Cell>
        <Cell>
          <Data ss:Type="Number">0.96726507412608</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">0000197334</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">test</Data>
        </Cell>
      </Row>
      <Row>
        <Cell>
          <Data ss:Type="String">XdpptxEYqEf</Data>
        </Cell>
        <Cell>
          <Data ss:Type="Number">0.734600760850404</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">0001564744</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">test</Data>
        </Cell>
      </Row>
      <Row>
        <Cell>
          <Data ss:Type="String">RbkWExnsMfJNHS</Data>
        </Cell>
        <Cell>
          <Data ss:Type="Number">0.666254851346023</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">0003279746</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">test</Data>
        </Cell>
      </Row>
      <Row>
        <Cell>
          <Data ss:Type="String">OBbRffRJP</Data>
        </Cell>
        <Cell>
          <Data ss:Type="Number">0.721114829052759</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">0007181989</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">test</Data>
        </Cell>
      </Row>
      <Row>
        <Cell>
          <Data ss:Type="String">ZHdLm MohfByWd</Data>
        </Cell>
        <Cell>
          <Data ss:Type="Number">0.270307757551925</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">0008739388</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">test</Data>
        </Cell>
      </Row>
      <Row>
        <Cell>
          <Data ss:Type="String">kNqGnHE</Data>
        </Cell>
        <Cell>
          <Data ss:Type="Number">0.895800942506548</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">0001980657</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">test</Data>
        </Cell>
      </Row>
      <Row>
        <Cell>
          <Data ss:Type="String">xYr yJdeKp</Data>
        </Cell>
        <Cell>
          <Data ss:Type="Number">0.462409074633573</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">0002466265</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">test</Data>
        </Cell>
      </Row>
      <Row>
        <Cell>
          <Data ss:Type="String">GZvyMV</Data>
        </Cell>
        <Cell>
          <Data ss:Type="Number">0.921151050795406</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">0000487429</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">test</Data>
        </Cell>
      </Row>
      <Row>
        <Cell>
          <Data ss:Type="String">AysRFgIMuUfwCE</Data>
        </Cell>
        <Cell>
          <Data ss:Type="Number">0.102547224193135</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">0009025478</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">test</Data>
        </Cell>
      </Row>
      <Row>
        <Cell>
          <Data ss:Type="String">RYaFkSvWhWd</Data>
        </Cell>
        <Cell>
          <Data ss:Type="Number">0.313286157936457</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">0007754057</Data>
        </Cell>
        <Cell>
          <Data ss:Type="String">test</Data>
        </Cell>
      </Row>
    </Table>
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
      <ProtectObjects>False</ProtectObjects>
      <ProtectScenarios>False</ProtectScenarios>
    </WorksheetOptions>
  </Worksheet>
</Workbook>
Reference : http://msdn.microsoft.com/en-us/library/bb226687%28v=office.11%29.aspx