songjianhui

刚刚开始做Excel相关的项目,所以遇到的问题不管大小都给记录一下

偶然的机会在添加数据的时候全改成了数字,结果输出的时候全自动变成了科学计数法,这是excel的强大功能,能自动识别数字和字符串,太聪明了反而有些麻烦,就像如果输入身份证(18位数字)的话那就不行了。超过了11位呢,下面查了些资料总结一下解决方案:

方法1:  在往excel中添加数据的时候在数据的前面加单引号或者其他字符、空格等,我的是这个方法解决的

 public static string ExportTable(ArrayList data, ArrayList columns)
        {

            ArrayList columnsBottom = getColumnsBottom(columns);

            ArrayList columnsTable = getColumnsTable(columns);

            StringBuilder sb = new StringBuilder();
            //data = ds.DataSetName + "\n";


            //data += tb.TableName + "\n";
            sb.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">");
            sb.AppendLine("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">");
            //写出列名

            for (int i = 0; i < columnsTable.Count; i++)
            {
                ArrayList columnsRow = (ArrayList)columnsTable[i];
                sb.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">");
                foreach (Hashtable column in columnsRow)
                {
                    sb.AppendLine("<td colspan=" + column["colspan"] + " rowspan=" + column["rowspan"] + ">" + column["header"] + "</td>");
                }
                sb.AppendLine("</tr>");
            }
            //写出数据

            int count = 0;
            foreach (Hashtable row in data)
            {
                sb.Append("<tr>");
                foreach (Hashtable column in columnsBottom)
                {
                    Object value;
                    if (column["field"] != null)
                    {
                        value = row[column["field"]];
                    }
                    else
                    {
                        value = "";
                    }
                    if (Convert.ToString(column["type"]) == "indexcolumn") value = count + 1;
                    if(Convert.ToString(column["field"]) == "Order_Code")
                    {
                        sb.AppendLine("<td>" + value + "&nbap;</td>");
                    }else
                    {
                        sb.AppendLine("<td >" + value + "</td>");
                    }
                }
                sb.AppendLine("</tr>");
                count++;
            }
            sb.AppendLine("</table>");


            return sb.ToString();
        }

 

方法2:在样式中添加样式::<td style=”vnd.ms-excel.numberformat:@”> 

 public static string ExportTable(ArrayList data, ArrayList columns)
        {

            ArrayList columnsBottom = getColumnsBottom(columns);

            ArrayList columnsTable = getColumnsTable(columns);

            StringBuilder sb = new StringBuilder();
            //data = ds.DataSetName + "\n";


            //data += tb.TableName + "\n";
            sb.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">");
            sb.AppendLine("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">");
            //写出列名

            for (int i = 0; i < columnsTable.Count; i++)
            {
                ArrayList columnsRow = (ArrayList)columnsTable[i];
                sb.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">");
                foreach (Hashtable column in columnsRow)
                {
                    sb.AppendLine("<td colspan=" + column["colspan"] + " rowspan=" + column["rowspan"] + ">" + column["header"] + "</td>");
                }
                sb.AppendLine("</tr>");
            }
            //写出数据

            int count = 0;
            foreach (Hashtable row in data)
            {
                sb.Append("<tr>");
                foreach (Hashtable column in columnsBottom)
                {
                    Object value;
                    if (column["field"] != null)
                    {
                        value = row[column["field"]];
                    }
                    else
                    {
                        value = "";
                    }
                    if (Convert.ToString(column["type"]) == "indexcolumn") value = count + 1;
                    if(Convert.ToString(column["field"]) == "Order_Code")
                    {
                        sb.AppendLine("<td style=\"vnd.ms-excel.numberformat:@\">" + value + "</td>");
                    }else
                    {
                        sb.AppendLine("<td >" + value + "</td>");
                    }
                }
                sb.AppendLine("</tr>");
                count++;
            }
            sb.AppendLine("</table>");


            return sb.ToString();
        }

 

方法3:

ADO.NET读Excel时,会根据该列的前n行数据(n个数来自注册表HKEY_LOCAL_MACHINE/Software/Microsoft/Jet/4.0/Engines/Excel/TypeGuessRows),根据这几行数据的内容来判断该列的类型,这个类型跟Excel中该列的类型无关(文本类型应该是数据前面有一个')。如果这n行中的数据有文本有数字,也就是混合类型,则根据HKEY_LOCAL_MACHINE/Software/Microsoft/Jet/4.0/Engines/Excel/ImportMixedType来取值。如果ImportMixedType值为Text,则该列为文本;如果值为Majority Type,则取数据多的类型。

为了能把有文本有数字的列正确读出,我们需要把注册表中ImportMixedType项设置为text,TypeGuessRows设置为0(表示要读取所有数据再来判断是否是混合类型)

另外需注意,IMEX的取值会影响是否使用注册表中ImportMixedType和MAXSCANROWS这两项,如果IMEX=1,则使用,如果为0或者2,则不使用。而IMEX=1是打开的只读连接,所以要正确读取,只能使用只读的方式。

分类:

技术点:

Excel

相关文章: