C#实现EXCEL数据转换到TXT文档

今天写了一段 EXCEL数据按一定格式转换到TXT文档的C#代码.在对EXCEL表的读取采用了.NET提供的OLEDB技术,对字符串的连接使用了StringBuilder 类.

excel中的数据格式如下:

设备名称 规格型号 设备编号  使用部门 固定资产编号
电脑1 IBM5660 10001 管理部 100010001
电脑2 IBM5661 10002 研发部 100010002
电脑3 IBM5662 10003 管理部 100010003

转换到TXT文档的格式:

"检测设备资产标签","设备名称","电脑1","规格型号","IBM5660","设备编号","10001","使用部门","管理部","固定资产编号","100010001"
"检测设备资产标签","设备名称","电脑2","规格型号","IBM5661","设备编号","10002","使用部门","研发部","固定资产编号","100010002"
"检测设备资产标签","设备名称","电脑3","规格型号","IBM5662","设备编号","10003","使用部门","管理部","固定资产编号","100010003"
end

页面设计代码:

namespace ExcelToTxt
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.dgvShow = new System.Windows.Forms.DataGridView();
            this.btnSelect = new System.Windows.Forms.Button();
            this.btnChange = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dgvShow)).BeginInit();
            this.SuspendLayout();
            //
            // dgvShow
            //
            this.dgvShow.AllowUserToAddRows = false;
            this.dgvShow.AllowUserToDeleteRows = false;
            this.dgvShow.AllowUserToResizeRows = false;
            this.dgvShow.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvShow.

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用C#中的NPOI库和DataGridView控件来将Excel数据转换并显示在DataGridView中。下面是一个示例代码: ```csharp using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System.Data; using System.Windows.Forms; public static class ExcelUtility { public static void ExcelToDataGridView(string filePath, DataGridView dataGridView) { IWorkbook workbook = null; ISheet sheet = null; DataTable data = new DataTable(); using (var file = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { if (filePath.EndsWith(".xls")) { workbook = new HSSFWorkbook(file); } else if (filePath.EndsWith(".xlsx")) { workbook = new XSSFWorkbook(file); } if (workbook != null) { sheet = workbook.GetSheetAt(0); if (sheet != null) { var firstRow = sheet.GetRow(0); int cellCount = firstRow.LastCellNum; for (int i = firstRow.FirstCellNum; i < cellCount; ++i) { var cell = firstRow.GetCell(i); if (cell != null) { string columnName = cell.ToString(); if (!string.IsNullOrEmpty(columnName)) { data.Columns.Add(columnName); } } } for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; ++i) { var row = sheet.GetRow(i); if (row != null) { bool emptyRow = true; DataRow dataRow = data.NewRow(); for (int j = row.FirstCellNum; j < cellCount; ++j) { if (row.GetCell(j) != null) { dataRow[j] = row.GetCell(j).ToString(); if (!string.IsNullOrEmpty(dataRow[j].ToString())) { emptyRow = false; } } } if (!emptyRow) { data.Rows.Add(dataRow); } } } } } } dataGridView.DataSource = data; } } ``` 这段代码会根据文件路径读取Excel文件,将第一个工作表转换为一个DataTable对象,并将该对象作为DataGridView控件的DataSource来显示Excel数据。你只需要将DataGridView控件添加到窗体中即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值