当前位置:编程学习 > C#/ASP.NET >>

c# 读取excel 单元格内容被截断

--------------------编程问答-------------------- 请参考:
http://blog.csdn.net/driftboat/archive/2010/03/05/5349945.aspx --------------------编程问答-------------------- 你已经添加了IMEX=1,所以不会出现截断现象了。
我测试过,的确没有截断,只不过用表格控件显示的时候会截断显示,显示不全,但是原始数据没有问题,DataTable中式正常的。 --------------------编程问答-------------------- 没遇到过这个问题. --------------------编程问答-------------------- 我测试的没问题


附程序源码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace testcheckbox
{
    public partial class excel : Form
    {
        public excel()
        {
            InitializeComponent();
        }

        private string xlsname = string.Empty;
        DataSet ds = new DataSet();
        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "EXCEL文件(*.xls)|*.xls";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                xlsname = ofd.FileName;
            }
            toolStripStatusLabel1.Text = xlsname;
            readxls(xlsname);
        }

        private void readxls(string args_xlsname)
        {
            string connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + args_xlsname + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\";";
            toolStripStatusLabel1.Text = connstr;
            using (OleDbConnection conn = new OleDbConnection(connstr))
            {
                using (OleDbDataAdapter da = new OleDbDataAdapter("Select * from [Sheet1$]", conn))
                {
                    da.Fill(ds,"excel");
                }
            }
            dataGridView1.DataSource = ds.Tables["excel"];
        }
    }
}

--------------------编程问答-------------------- 为了使DataGridView显示全你的数据,我将DataGridView的样式设置为:

在设计模式选中DataGridView1,选择属性,设置DefaultCellStyle属性里的WrapMode属性,默认为false,设置为true。
设置AutoSizeRowsMode属性为:DisplayedCellsExceptHeaders。

这样就能完全显示你的数据了
其中拖了2个空间,打开选择excel文件的ToolStripLabel1供打开选择文件
toolStripStatusLabel1显示构造的链接字符串是否正确

测试通过,你看看你的代码是否有问题? --------------------编程问答--------------------
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,