今天写了一段 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.