特别注意: range.Text.ToString(); 和 range.Value2.ToString(); 的区别

如果Excell的单元格里面是日期,前面显示2015年05月10日;后面的显示42134

也就是说:Text 和Value2的不同。

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;

public partial class dingdan2_Default5 : System.Web.UI.Page
{
    string str日期;
    string str单号;
    string str运费支付方式;
    string str收货人;
    string str电话;
    string str购货单位;
    string str收货地址;
    string str制单人;
    string str销售业务员;
    string str财务部;
    string str物流货运组;
    string str仓储组;

    string[,] str索引 = new string[8,10];
    string[,] str数据 = new string[8,10];

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            ((MasterPage)Master).setDaohang("订单管理->导入订单");

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if ((FileUpload1.HasFile) && (null != FileUpload1.PostedFile))
        {
            this.ListBox1.Items.Clear();

            String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            String[] allowedExtensions = { ".xls" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension.ToLower() == allowedExtensions[i])
                {
                   this.Session["g_filename"] = FileUpload1.FileName;
                    this.FileUpload1.SaveAs(Server.MapPath("uploadfiles\\") + FileUpload1.FileName);
                    this.ListBox1.Items.Insert(0, "文件上传成功!   " + FileUpload1.FileName);
                }
                else
                {
                    this.ListBox1.Items.Insert(0, "只能上传EXCEL文件!" + FileUpload1.FileName);
                }

            }
        }
        else
        {
            this.ListBox1.Items.Insert(0, "无法检测到上传的文件!");
        }

    }
    //Excel 应用程序对象
    Excel.Application ExcelRS;
    //Excel 工作簿对象
    Excel._Workbook RSbook;
    //Excel 工作页对象
    Excel._Worksheet RSsheet;
    //某一个单元格对象
    Excel.Range range;

    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            //第一步:建立Excel应用程序
            ExcelRS = new Excel.Application();
            this.ListBox1.Items.Insert(0, "建立Excel应用程序成功!");

            //第二步:建立Excel工作簿
            RSbook = (Excel._Workbook)ExcelRS.Workbooks.Open(Server.MapPath("uploadfiles\\")+this.Session["g_filename"].ToString(), Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            this.ListBox1.Items.Insert(0, "建立Excel工作簿成功!");

            //第三步:设置第一个工作溥
            RSsheet = (Excel._Worksheet)RSbook.Sheets.get_Item(1);
            this.ListBox1.Items.Insert(0, "建立Excel工作页成功!");

            //第四步:读取数据
            range = (Excel.Range)RSsheet.get_Range("B2", Type.Missing);
            this.str日期 = range.Text.ToString();
            this.ListBox1.Items.Insert(0,"日期:"+this.str日期);

            range = (Excel.Range)RSsheet.get_Range("G2", Type.Missing);
            this.str单号= range.Value2.ToString();
            this.ListBox1.Items.Insert(0, "单号:" + this.str单号);

            range = (Excel.Range)RSsheet.get_Range("B3", Type.Missing);
            this.str运费支付方式 = range.Value2.ToString();
            this.ListBox1.Items.Insert(0, "运费支付方式:" + this.str运费支付方式);

            range = (Excel.Range)RSsheet.get_Range("D3", Type.Missing);
            this.str收货人 = range.Value2.ToString();
            this.ListBox1.Items.Insert(0, "收货人:" + this.str收货人);

            range = (Excel.Range)RSsheet.get_Range("G3", Type.Missing);
            this.str电话 = range.Value2.ToString();
            this.ListBox1.Items.Insert(0, "电话:" + this.str电话);

            range = (Excel.Range)RSsheet.get_Range("B4", Type.Missing);
            this.str购货单位 = range.Value2.ToString();
            this.ListBox1.Items.Insert(0, "购货单位:" + this.str购货单位);

            range = (Excel.Range)RSsheet.get_Range("B5", Type.Missing);
            this.str收货地址 = range.Value2.ToString();
            this.ListBox1.Items.Insert(0, "收货地址:" + this.str收货地址);

            range = (Excel.Range)RSsheet.get_Range("B15", Type.Missing);
            this.str制单人 = range.Value2.ToString();
            this.ListBox1.Items.Insert(0, "制单人:" + this.str制单人);

            range = (Excel.Range)RSsheet.get_Range("E15", Type.Missing);
            this.str销售业务员 = range.Value2.ToString();
            this.ListBox1.Items.Insert(0, "销售业务员:" + this.str销售业务员);

            range = (Excel.Range)RSsheet.get_Range("I15", Type.Missing);
            this.str财务部 = range.Value2.ToString();
            this.ListBox1.Items.Insert(0, "财务部:" + this.str财务部);

            range = (Excel.Range)RSsheet.get_Range("B16", Type.Missing);
            this.str物流货运组 = range.Value2.ToString();
            this.ListBox1.Items.Insert(0, "物流货运组:" + this.str物流货运组);

            range = (Excel.Range)RSsheet.get_Range("E16", Type.Missing);
            this.str仓储组 = range.Value2.ToString();
            this.ListBox1.Items.Insert(0, "仓储组:" + this.str仓储组);

            for (int i = 7; i < 15; i++)
            {
                str索引[i - 7, 0] = "A" + i.ToString();
                str索引[i - 7, 1] = "B" + i.ToString();
                str索引[i - 7, 2] = "C" + i.ToString();
                str索引[i - 7, 3] = "D" + i.ToString();
                str索引[i - 7, 4] = "E" + i.ToString();
                str索引[i - 7, 5] = "F" + i.ToString();
                str索引[i - 7, 6] = "G" + i.ToString();
                str索引[i - 7, 7] = "H" + i.ToString();
                str索引[i - 7, 8] = "I" + i.ToString();
                str索引[i - 7, 9] = "J" + i.ToString();
            }

            //读取数据
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    range  = (Excel.Range)RSsheet.get_Range(str索引[i,j], Type.Missing);
                    str数据[i,j] = range.Text.ToString();
                    this.ListBox1.Items.Add(i.ToString() + ":" + j.ToString() + "=" + str数据[i, j]);
                }
            }

        }
        catch (System.Exception ex)
        {
            this.ListBox1.Items.Insert(0,"异常! "+ex.ToString() + "-" +ex.Message +"-"+ex.Source);
        }
        finally
        {
            ExcelRS.Quit();
        }

    }
}
时间: 2024-08-07 19:46:20

特别注意: range.Text.ToString(); 和 range.Value2.ToString(); 的区别的相关文章

【转载ToString()转换格式;DateTime.ToString()用法详解】

ToString()转换格式;DateTime.ToString()用法详解 格式模式 说明和关联属性 c.C 货币格式.关联的属性包括: CurrencyDecimalDigits, CurrencyDecimalSeparator, CurrencyGroupSeparator, CurrencyGroupSizes, CurrencyNegativePattern, CurrencyPositivePattern, CurrencySymbol. d.D 十进制格式. e.E 科学计数(指

thymeleaf中th:text和th:utext的使用与区别

thymeleaf中th:text的使用, 获取方法: 关于中th:text和th:utext的使用与区别,少废话直接看结果: 用两种方法获取 输出的最终结果: 原文地址:https://www.cnblogs.com/lipu12281/p/12257125.html

[LeetCode] Range Sum Query - Immutable &amp; Range Sum Query 2D - Immutable

Range Sum Query - Immutable Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note: You

[leetcode] 303. Range Sum Query &amp;&amp; 304. Range Sum Query 2D - Immutable

Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums = [-2, 0, 3, -5, 2, -1] sumRange(0, 2) -> 1 sumRange(2, 5) -> -1 sumRange(0, 5) -> -3 Note: You may assume that the array do

[email&#160;protected] [303/304] Range Sum Query - Immutable / Range Sum Query 2D - Immutable

https://leetcode.com/problems/range-sum-query-immutable/ class NumArray { public: vector<int> vec; public: NumArray(vector<int> &nums) { if(nums.empty()) return; else { vec.push_back(nums[0]); for(int i=1;i<nums.size();++i) vec.push_bac

Java 异常Exception e中e的getMessage()和toString()以及 e.printStackTrace();方法的区别

Exception e中e的getMessage()和toString()方法的区别: 示例代码1: public class TestInfo {     private static String str =null;     public static void main(String[] args) {         System.out.println("test exception");         try {             if(str.equals(&q

JS中toString()、toLocaleString()、valueOf()的区别

前言 Array.Boolean.Date.Number等对象都具有 toString().toLocaleString().valueOf()三个方法,那这三个方法有什么区别? 一.JS Array 1)举例 var array = new Array("niu","li","na"); console.log(array.valueOf()); console.log(array.toString()); console.log(array.

微信小程序 input 的 type属性 text、number、idcard、digit 区别

微信小程序的 input 有个属性叫 type,这个 type 有几个可选值: text:不必解释 number:数字键盘(无小数点) idcard:数字键盘(无小数点.有个 X 键) digit:数字键盘(有小数点) 注意:number 是无小数点的,digit 是有小数点的 输入时键盘只能出现相应的数字键盘 转发自:https://blog.csdn.net/yelin042/article/details/72519138 原文地址:https://www.cnblogs.com/firs

C#生成Word报表(插入文字、表格和图片)

回家前一个晚上,老板临时安排让写一个读取txt文件的数据和多个图片数据生成一个word文档.时间给得太短只能是回家继续加班,百度真是个好东西这里引用一下我参考的博客http://blog.csdn.net/jiutao_tang/article/details/6574740/ http://xuzhihong1987.blog.163.com/blog/static/2673158720109188465634/  这个博客讲的用录制word宏的方法做参考很有用.感谢这位博友 静下心,认真做,