Ich muss eine EXE erstellen. Datei in Visual Studio 2015 C#, die exe. Datei muss eine Excel-Datei lesen und eine bestimmte Spalte daraus auswählen und dann diese bestimmte Spalte als neue Vorlage ausgeben.Wie scannt man eine bestimmte Zeichenkette von Excel und gibt die Ausgabe als neue Vorlage aus?
Sagen wir mal, ein Excel hat ursprünglich Spalte Name, Geschlecht, Alter. Ich möchte die exe verwenden, um alle Informationen nur in Spaltenname auszuwählen, und es als eine andere neue Excel-Datei ausgeben.
Jetzt kann ich Excel-Datei lesen und es als eine andere Excel ausgeben, aber ich weiß nicht, wie man eine bestimmte Zeichenkette von Excel scannt und die Ausgabe als neue Vorlage ist?
Kodex OP von mispost:
using System;
using System.IO;
using System.Data.OleDb;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using Spire.Xls;
using Spire.Xls.Charts;
namespace Spire.Xls.Sample
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnRun;
private System.Windows.Forms.Button btnPDF;
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Label label1;
public DataGrid dataGrid1;
/// <summary>
/// Required designer variable.
/// </summary
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if(disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnRun = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.btnPDF = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// btnRun
//
this.btnRun.Location = new System.Drawing.Point(313, 494);
this.btnRun.Name = "btnRun";
this.btnRun.Size = new System.Drawing.Size(144, 30);
this.btnRun.TabIndex = 2;
this.btnRun.Text = "Apply filter";
this.btnRun.Click += new System.EventHandler(this.btnRun_Click);
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(633, 494);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(75, 30);
this.btnClose.TabIndex = 3;
this.btnClose.Text = "Close";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// label1
//
this.label1.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.Location = new System.Drawing.Point(16, 14);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(528, 41);
this.label1.TabIndex = 4;
this.label1.Text = "This app can apply filter to Excel";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// dataGrid1
//
this.dataGrid1.AllowDrop = true;
this.dataGrid1.DataMember = "Group";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.ImeMode = System.Windows.Forms.ImeMode.On;
this.dataGrid1.Location = new System.Drawing.Point(313, 221);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(406, 249);
this.dataGrid1.TabIndex = 5;
//
// btnPDF
//
this.btnPDF.Location = new System.Drawing.Point(474, 494);
this.btnPDF.Name = "btnPDF";
this.btnPDF.Size = new System.Drawing.Size(144, 30);
this.btnPDF.TabIndex = 6;
this.btnPDF.Text = "Save as PDF";
this.btnPDF.Click += new System.EventHandler(this.btnPDF_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 18);
this.ClientSize = new System.Drawing.Size(731, 552);
this.Controls.Add(this.btnPDF);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnRun);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Excel modifier";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
private void label1_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btnRun_Click(object sender, System.EventArgs e)
{
Workbook workbook = new Workbook();
//Initailize worksheet
Worksheet sheet = workbook.Worksheets[0];
sheet.InsertDataTable((DataTable)this.dataGrid1.DataSource,true,1,1,-1,-1);
//Sets body style
CellStyle oddStyle = workbook.Styles.Add("oddStyle");
oddStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
oddStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
oddStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
oddStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
oddStyle.KnownColor = ExcelColors.LightGreen1;
CellStyle evenStyle = workbook.Styles.Add("evenStyle");
evenStyle.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
evenStyle.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
evenStyle.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
evenStyle.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
evenStyle.KnownColor = ExcelColors.LightTurquoise;
foreach(CellRange range in sheet.AllocatedRange.Rows)
{
if (range.Row % 2 == 0)
range.CellStyleName = evenStyle.Name;
else
range.CellStyleName = oddStyle.Name;
}
//Sets header style
CellStyle styleHeader = sheet.Rows[0].Style;
styleHeader.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
styleHeader.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
styleHeader.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
styleHeader.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
styleHeader.VerticalAlignment = VerticalAlignType.Center;
styleHeader.KnownColor = ExcelColors.Green;
styleHeader.Font.KnownColor = ExcelColors.White;
styleHeader.Font.IsBold = true;
sheet.AllocatedRange.AutoFitColumns();
sheet.AllocatedRange.AutoFitRows();
sheet.Rows[0].RowHeight = 20;
//Writes filter data
//CreateFilterData(sheet);
sheet.AutoFilters.Range = sheet.Range["A1:Z1"];
//append the sort column index and order by attributes
//workbook.DataSorter.SortColumns.Add(2, OrderBy.Ascending);
//workbook.DataSorter.SortColumns.Add(3, OrderBy.Ascending);
//set the range to sort.
//workbook.DataSorter.Sort(sheet["A1:Z999"]);
//save the xls to this path
workbook.SaveToFile(@"C:\Users\SmartEducationCoLtd\Desktop\sample.xls");
ExcelDocViewer(workbook.FileName);
//private void CreateFilterData(Worksheet sheet)
//{ }
private void btnClose_Click(object sender, System.EventArgs e)
{
Close();
}
private void Form1_Load(object sender, System.EventArgs e)
{
Workbook workbook = new Workbook();
//load from this excel path
workbook.LoadFromFile(@"C:\Users\SmartEducationCoLtd\Desktop\DataTableSample.xls");
//Initailize worksheet
Worksheet sheet = workbook.Worksheets[0];
this.dataGrid1.DataSource = sheet.ExportDataTable();
}
private void ExcelDocViewer(string fileName)
{
try
{
System.Diagnostics.Process.Start(fileName);
}
catch{}
}
}
Wenn Sie sagen, Sie können eine Excel-Datei lesen, welchen Typ holen Sie es als? Veröffentlichen Sie einen Code von dem, was Sie getan haben, und wir können versuchen, String-Informationen daraus abzurufen. – Ash
Eine Excel-Datei mit drei Spalten (Name (Zeichenfolge), Geschlecht (Zeichenfolge), Alter (Zeichenfolge)) und in jeder Spalte sind z. Jason (String), M (String), 22 (int) –
Ich benutze Spire.Xls; um eine Excel-Datei zu lesen und als eine weitere neue Excel-Datei auszugeben –