using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Web; using System.Reflection; using System.Xml.Linq; using System.Collections; namespace WriteExcelF { //Excel //OpenFileDialog public partial class Form1 : Form { public Form1() { InitializeComponent(); groupChoose.Visible = false; groupChoose.Location = new Point(0, 25); panelResult.Visible = true; panelResult.Location = new Point(3, 25); int xWidth = Screen.PrimaryScreen.WorkingArea.Width; int yHeight = Screen.PrimaryScreen.WorkingArea.Height; this.StartPosition = FormStartPosition.Manual; int X = xWidth - Width - 4; int Y = yHeight - Height - 4; this.Location = new Point(X, Y); txtColumn1.Text = "A"; txtColumn2.Text = "B"; txtRow1.Text = "1"; txtRow2.Text = "1"; Row1 = Convert.ToInt32(txtRow1.Text); Row2 = Convert.ToInt32(txtRow2.Text); strColumn1 = txtColumn1.Text.Trim(); strColumn2 = txtColumn2.Text.Trim(); lbRow1.Text = "ResultRow: " + Row1.ToString(); lbColumn.Text = "ResultColumn: " + strColumn1; txtRow.Text = Row2.ToString(); txtColumn.Text = strColumn2; txtSheetName.Text = "Sheet1"; } //Test Result private int Row1 = 1; //Test Info private int Row2 = 1; //Excel Path string FilePath = @"D:\TestResult\TestResult.xls"; //Excel Sheet Name string SheetName = "Sheet1"; string StrResultName = @"D:\TestResult\TestResult.xml"; string StrCopyResultName = @"D:\TestResult\CopyTestResult.xml"; //Get Result String private void GetString(CheckBox checkbox, TextBox txtbox, ref string TestInfo) { if (txtbox.Text.Trim() != "") { TestInfo += checkbox.Text; txtbox.Text = txtbox.Text.Replace("\r\n", "\r\n "); TestInfo += txtbox.Text; TestInfo += "\r\n"; } } //Button Commit Function private void Commit_Click(object sender, EventArgs e) { try { Row2 = Convert.ToInt32(txtRow.Text); Row1 = Row2; strColumn2 = txtColumn.Text; } catch (Exception) { MessageBox.Show("Not Null"); return; } try { XElement xe = XElement.Load(StrResultName); IEnumerable<XElement> elements1 = from element in xe.Elements("File") select element; DateTime date1 = DateTime.Now; string StrDate = date1.Year.ToString() + " : " + date1.Month.ToString() + " : " + date1.Day.ToString(); string TestInfo1 = ""; string TestInfo2 = ""; string TestInfo3 = ""; string TestInfo4 = ""; string TestInfo5 = ""; string TestInfo6 = ""; string TestInfo7 = ""; string TestInfo8 = ""; GetString(checkBanben, txtBanben, ref TestInfo1); GetString(checkModel, txtModel, ref TestInfo2); GetString(checkHuamian, txtHuamian, ref TestInfo3); GetString(checkBupin, txtBupin, ref TestInfo4); GetString(checkQianti, txtQianti, ref TestInfo5); GetString(checkShoushun, txtShoushun, ref TestInfo6); GetString(checkXian, txtXian, ref TestInfo7); GetString(checkQidai, txtQidai, ref TestInfo8); XElement file = new XElement("Info", new XElement("Date", StrDate), new XElement("Row1", Row1.ToString()), new XElement("Column1", strColumn1), new XElement("Row2", Row2.ToString()), new XElement("Column2", strColumn2), new XElement("Banben", TestInfo1), new XElement("Model", TestInfo2), new XElement("Huamian", TestInfo3), new XElement("Bupin", TestInfo4), new XElement("Qianti", TestInfo5), new XElement("Shoushun", TestInfo6), new XElement("Xianxiang", TestInfo7), new XElement("Qidai", TestInfo8), new XElement("TestResult", cmbResult.Text) ); xe.Add(file); xe.Save(StrResultName); MessageBox.Show("Save Sucess"); } catch(Exception ex) { MessageBox.Show(ex.ToString()); return; } Clear(); } //Clear TextBox private void Clear() { txtRow.Text = (Convert.ToInt32(txtRow.Text) + 1).ToString(); lbRow1.Text = "ResultRow: " + txtRow.Text; CheckClear(checkBanben, txtBanben); CheckClear(checkModel, txtModel); CheckClear(checkHuamian, txtHuamian); CheckClear(checkBupin, txtBupin); CheckClear(checkQianti, txtQianti); CheckClear(checkShoushun, txtShoushun); CheckClear(checkXian, txtXian); CheckClear(checkQidai, txtQidai); txtRow.Focus(); } //Check Is Or Not Clear private void CheckClear(CheckBox checkbox, TextBox txtbox) { if (checkbox.Checked) { txtbox.Text = txtbox.Text; } else { txtbox.Text = ""; } } //Set Excel Cell Width public void SetColumnWidth(Microsoft.Office.Interop.Excel._Worksheet CurSheet, string strColID, double dblWidth) { ((Microsoft.Office.Interop.Excel.Range)CurSheet.Columns.GetType().InvokeMember("Item", System.Reflection.BindingFlags.GetProperty, null, CurSheet.Columns, new object[] { (strColID + ":" + strColID).ToString() })).ColumnWidth = dblWidth; } /// <summary> /// Set Column Auto /// </summary> /// <param name="columnNum">Column</param> public void ColumnAutoFit(Microsoft.Office.Interop.Excel.Worksheet worksheet, string strcolumnNum) { Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)worksheet.Columns[strcolumnNum + ":" + strcolumnNum, System.Type.Missing]; range.EntireColumn.AutoFit(); } /// <summary> /// Set Row Auto /// </summary> /// <param name="RowNum">Row</param> public void RowAutoFit(Microsoft.Office.Interop.Excel.Worksheet worksheet, int rowNum) { Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)worksheet.Rows[rowNum.ToString() + ":" + rowNum.ToString(), System.Type.Missing]; range.EntireColumn.AutoFit(); } //Input Excel Result public bool InputExcel(int Row1,string strColumn1, int Row2, string strColumn2, string TestResult, string TestInfo) { bool Result = false; Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application(); //Microsoft.Office.Interop.Excel.Application ExcelApp = null; Microsoft.Office.Interop.Excel.Workbook WorkBooks = null; Microsoft.Office.Interop.Excel.Worksheet WorkSheet = null; try { // ExcelApp = new Microsoft.Office.Interop.Excel.Application(); WorkBooks = ExcelApp.Workbooks._Open(FilePath, Missing.Value, Missing.Value, Missing.Value , Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); WorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)WorkBooks.Worksheets[SheetName]; ColumnAutoFit(WorkSheet, strColumn1); ColumnAutoFit(WorkSheet, strColumn2); RowAutoFit(WorkSheet, Row1); RowAutoFit(WorkSheet, Row2); WorkSheet.Cells[Row1, strColumn1] = TestResult; WorkSheet.Cells[Row2, strColumn2] = TestInfo; ExcelApp.Application.DisplayAlerts = false; WorkBooks.Save(); Dispose(WorkSheet, WorkBooks, ExcelApp); Result = true; } catch (Exception ex) { MessageBox.Show(ex.ToString()); Dispose(WorkSheet, WorkBooks, ExcelApp); Result = false; } finally { //if (ExcelApp != null) //{ // Dispose(WorkSheet, WorkBooks, ExcelApp); //} } return Result; } //Dispose Excel Resource public void Dispose(Microsoft.Office.Interop.Excel._Worksheet CurSheet, Microsoft.Office.Interop.Excel._Workbook CurBook, Microsoft.Office.Interop.Excel._Application CurExcel) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(CurSheet); CurSheet = null; CurBook.Close(false, Missing.Value, Missing.Value); System.Runtime.InteropServices.Marshal.ReleaseComObject(CurBook); CurBook = null; CurExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(CurExcel); CurExcel = null; GC.Collect(); GC.WaitForPendingFinalizers(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); //throw new Exception(ex.Message); } finally { //foreach (System.Diagnostics.Process pro in System.Diagnostics.Process.GetProcessesByName("Excel")) // //if (pro.StartTime < DateTime.Now) // pro.Kill(); } System.GC.SuppressFinalize(this); } //Open Group private void toolStripButton1_Click(object sender, EventArgs e) { if (MessageBox.Show("Choose File?", "FilePath", MessageBoxButtons.OKCancel, MessageBoxIcon.None) == DialogResult.OK) { groupChoose.Visible = true; timer1.Enabled = false; ChangOk = false; ChangeOpen = false; strTitle = ""; this.Text = "FileName: "; btnOpen.Focus(); } } int TitleLength = 0; bool ChangOk = false; bool ChangeOpen = false; //Open Excel File private void btnOpen_Click(object sender, EventArgs e) { try { this.Text = this.Text.Remove(9); OpenFileDialog OpenFile = new OpenFileDialog(); OpenFile.Filter = "Excel|*.xls"; OpenFile.Multiselect = false; if (OpenFile.ShowDialog() == DialogResult.OK) { FilePath = OpenFile.FileName; txtFileName.Text = OpenFile.FileName; this.Text += " " + OpenFile.SafeFileName + " "; TitleLength = this.Text.Length; } ChangeOpen = true; } catch (Exception ex) { ChangeOpen = false; } } string strColumn1 = ""; string strColumn2 = ""; //Set Row And Column private void btnColumn_Click(object sender, EventArgs e) { try { this.Text = this.Text.Remove(TitleLength - 1); Row1 = Convert.ToInt32(txtRow1.Text); Row2 = Convert.ToInt32(txtRow2.Text); strColumn1 = txtColumn1.Text.Trim(); strColumn2 = txtColumn2.Text.Trim(); lbRow1.Text = "ResultRow: " + Row1.ToString(); lbColumn.Text = "ResultColumn: " + strColumn1; txtRow.Text = Row2.ToString(); txtColumn.Text = strColumn2; List<string> list = new List<string>(); string[] strPath = txtClass.Text.Replace("\r\n", "~").Split('~'); foreach (string str in strPath) { list.Add(str); } cmbResult.DataSource = null; cmbResult.DataSource = list; if (txtSheetName.Text.Trim() == "") { int a = Convert.ToInt32("dfd"); } else { SheetName = txtSheetName.Text; this.Text += " SheetName: " + SheetName; } ChangOk = true; } catch (Exception ex) { ChangOk = false; } } //Begin Test private void btnVisible_Click(object sender, EventArgs e) { if (ChangOk && ChangeOpen) { groupChoose.Visible = false; panelResult.Visible = true; if (this.Text.Length > 40) { timer1.Enabled = true; } txtRow.Focus(); } else { MessageBox.Show("Choose Or Column"); } } //Gain Row1 Row2 private void txtRow_Leave(object sender, EventArgs e) { lbRow1.Text = "ResultRow: " + txtRow.Text; } //Gain Title string strTitle = ""; private void timer1_Tick(object sender, EventArgs e) { if (this.Text.Length > 0) { strTitle += this.Text[0]; this.Text = this.Text.Remove(0, 1); } else { this.Text = strTitle; strTitle = ""; } } private void Save_Click(object sender, EventArgs e) { XElement xe2 = XElement.Load(StrResultName); IEnumerable<XElement> elements2 = from PFile in xe2.Elements("Info") select PFile; string TestResult = ""; string TestInfo = ""; XElement xe = XElement.Load(StrCopyResultName); IEnumerable<XElement> elements1 = from element in xe.Elements("File") select element; foreach (XElement element in elements2) { string StrDate = element.Element("Date").Value; int Row1 = Convert.ToInt32(element.Element("Row1").Value); string strColumn1 = element.Element("Column1").Value; int Row2 = Convert.ToInt32(element.Element("Row2").Value); string strColumn2 = element.Element("Column2").Value; string str5 = element.Element("Banben").Value; string str6 = element.Element("Model").Value; string str7 = element.Element("Huamian").Value; string str8 = element.Element("Bupin").Value; string str9 = element.Element("Qianti").Value; string str10 = element.Element("Shoushun").Value; string str11 = element.Element("Xianxiang").Value; string str12 = element.Element("Qidai").Value; string str13 = element.Element("TestResult").Value; XElement file = new XElement("Info", new XElement("Date", StrDate), new XElement("Row1", Row1.ToString()), new XElement("Column1", strColumn1), new XElement("Row2", Row2.ToString()), new XElement("Column2", strColumn2), new XElement("Banben", str5), new XElement("Model", str6), new XElement("Huamian", str7), new XElement("Bupin", str8), new XElement("Qianti", str9), new XElement("Shoushun", str10), new XElement("Xianxiang", str11), new XElement("Qidai", str12), new XElement("TestResult", str13) ); xe.Add(file); TestInfo = str5 + str6 + str7 + str8 + str9 + str10 + str11 + str12; TestResult = str13; InputExcel(Row1, strColumn1, Row2, strColumn2, TestResult, TestInfo); } xe.Save(StrCopyResultName); xe2.RemoveAll(); xe2.Save(StrResultName); MessageBox.Show("Save Sucess"); } } }
namespace WriteExcelF { 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.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.checkHuamian = new System.Windows.Forms.CheckBox(); this.checkBupin = new System.Windows.Forms.CheckBox(); this.checkBanben = new System.Windows.Forms.CheckBox(); this.checkModel = new System.Windows.Forms.CheckBox(); this.checkQianti = new System.Windows.Forms.CheckBox(); this.checkQidai = new System.Windows.Forms.CheckBox(); this.checkShoushun = new System.Windows.Forms.CheckBox(); this.checkXian = new System.Windows.Forms.CheckBox(); this.txtBanben = new System.Windows.Forms.TextBox(); this.txtModel = new System.Windows.Forms.TextBox(); this.txtHuamian = new System.Windows.Forms.TextBox(); this.txtBupin = new System.Windows.Forms.TextBox(); this.txtQianti = new System.Windows.Forms.TextBox(); this.txtShoushun = new System.Windows.Forms.TextBox(); this.txtXian = new System.Windows.Forms.TextBox(); this.txtQidai = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.txtRow = new System.Windows.Forms.TextBox(); this.txtColumn = new System.Windows.Forms.TextBox(); this.Commit = new System.Windows.Forms.Button(); this.cmbResult = new System.Windows.Forms.ComboBox(); this.label3 = new System.Windows.Forms.Label(); this.groupChoose = new System.Windows.Forms.GroupBox(); this.txtColumn2 = new System.Windows.Forms.TextBox(); this.txtRow2 = new System.Windows.Forms.TextBox(); this.label10 = new System.Windows.Forms.Label(); this.label9 = new System.Windows.Forms.Label(); this.txtClass = new System.Windows.Forms.TextBox(); this.label8 = new System.Windows.Forms.Label(); this.txtSheetName = new System.Windows.Forms.TextBox(); this.label7 = new System.Windows.Forms.Label(); this.btnVisible = new System.Windows.Forms.Button(); this.btnColumn = new System.Windows.Forms.Button(); this.txtColumn1 = new System.Windows.Forms.TextBox(); this.txtRow1 = new System.Windows.Forms.TextBox(); this.label6 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label(); this.btnOpen = new System.Windows.Forms.Button(); this.txtFileName = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); this.lbRow1 = new System.Windows.Forms.Label(); this.lbColumn = new System.Windows.Forms.Label(); this.panelResult = new System.Windows.Forms.Panel(); this.toolStrip1 = new System.Windows.Forms.ToolStrip(); this.toolStripButton1 = new System.Windows.Forms.ToolStripButton(); this.Save = new System.Windows.Forms.ToolStripButton(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.groupChoose.SuspendLayout(); this.panelResult.SuspendLayout(); this.toolStrip1.SuspendLayout(); this.SuspendLayout(); // // checkHuamian // this.checkHuamian.AutoSize = true; this.checkHuamian.Location = new System.Drawing.Point(1, 144); this.checkHuamian.Name = "checkHuamian"; this.checkHuamian.Size = new System.Drawing.Size(54, 16); this.checkHuamian.TabIndex = 0; this.checkHuamian.Text = "画面:"; this.checkHuamian.UseVisualStyleBackColor = true; // // checkBupin // this.checkBupin.AutoSize = true; this.checkBupin.Location = new System.Drawing.Point(1, 195); this.checkBupin.Name = "checkBupin"; this.checkBupin.Size = new System.Drawing.Size(54, 16); this.checkBupin.TabIndex = 0; this.checkBupin.Text = "部品:"; this.checkBupin.UseVisualStyleBackColor = true; // // checkBanben // this.checkBanben.AutoSize = true; this.checkBanben.Location = new System.Drawing.Point(1, 71); this.checkBanben.Name = "checkBanben"; this.checkBanben.Size = new System.Drawing.Size(54, 16); this.checkBanben.TabIndex = 0; this.checkBanben.Text = "版本:"; this.checkBanben.UseVisualStyleBackColor = true; // // checkModel // this.checkModel.AutoSize = true; this.checkModel.Location = new System.Drawing.Point(1, 103); this.checkModel.Name = "checkModel"; this.checkModel.Size = new System.Drawing.Size(54, 16); this.checkModel.TabIndex = 0; this.checkModel.Text = "模型:"; this.checkModel.UseVisualStyleBackColor = true; // // checkQianti // this.checkQianti.AutoSize = true; this.checkQianti.Location = new System.Drawing.Point(1, 247); this.checkQianti.Name = "checkQianti"; this.checkQianti.Size = new System.Drawing.Size(54, 16); this.checkQianti.TabIndex = 0; this.checkQianti.Text = "前提:"; this.checkQianti.UseVisualStyleBackColor = true; // // checkQidai // this.checkQidai.AutoSize = true; this.checkQidai.Location = new System.Drawing.Point(179, 193); this.checkQidai.Name = "checkQidai"; this.checkQidai.Size = new System.Drawing.Size(54, 16); this.checkQidai.TabIndex = 0; this.checkQidai.Text = "期待:"; this.checkQidai.UseVisualStyleBackColor = true; // // checkShoushun // this.checkShoushun.AutoSize = true; this.checkShoushun.Location = new System.Drawing.Point(179, 84); this.checkShoushun.Name = "checkShoushun"; this.checkShoushun.Size = new System.Drawing.Size(54, 16); this.checkShoushun.TabIndex = 0; this.checkShoushun.Text = "手顺:"; this.checkShoushun.UseVisualStyleBackColor = true; // // checkXian // this.checkXian.AutoSize = true; this.checkXian.Location = new System.Drawing.Point(179, 144); this.checkXian.Name = "checkXian"; this.checkXian.Size = new System.Drawing.Size(54, 16); this.checkXian.TabIndex = 0; this.checkXian.Text = "现象:"; this.checkXian.UseVisualStyleBackColor = true; // // txtBanben // this.txtBanben.Location = new System.Drawing.Point(54, 69); this.txtBanben.Name = "txtBanben"; this.txtBanben.Size = new System.Drawing.Size(100, 21); this.txtBanben.TabIndex = 5; // // txtModel // this.txtModel.Location = new System.Drawing.Point(54, 101); this.txtModel.Name = "txtModel"; this.txtModel.Size = new System.Drawing.Size(100, 21); this.txtModel.TabIndex = 6; // // txtHuamian // this.txtHuamian.Location = new System.Drawing.Point(54, 131); this.txtHuamian.Multiline = true; this.txtHuamian.Name = "txtHuamian"; this.txtHuamian.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.txtHuamian.Size = new System.Drawing.Size(120, 42); this.txtHuamian.TabIndex = 7; // // txtBupin // this.txtBupin.Location = new System.Drawing.Point(54, 182); this.txtBupin.Multiline = true; this.txtBupin.Name = "txtBupin"; this.txtBupin.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.txtBupin.Size = new System.Drawing.Size(120, 42); this.txtBupin.TabIndex = 8; // // txtQianti // this.txtQianti.Location = new System.Drawing.Point(54, 234); this.txtQianti.Multiline = true; this.txtQianti.Name = "txtQianti"; this.txtQianti.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.txtQianti.Size = new System.Drawing.Size(120, 42); this.txtQianti.TabIndex = 9; // // txtShoushun // this.txtShoushun.Location = new System.Drawing.Point(240, 61); this.txtShoushun.Multiline = true; this.txtShoushun.Name = "txtShoushun"; this.txtShoushun.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.txtShoushun.Size = new System.Drawing.Size(120, 63); this.txtShoushun.TabIndex = 10; // // txtXian // this.txtXian.Location = new System.Drawing.Point(240, 131); this.txtXian.Multiline = true; this.txtXian.Name = "txtXian"; this.txtXian.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.txtXian.Size = new System.Drawing.Size(120, 42); this.txtXian.TabIndex = 11; // // txtQidai // this.txtQidai.Location = new System.Drawing.Point(240, 180); this.txtQidai.Multiline = true; this.txtQidai.Name = "txtQidai"; this.txtQidai.ScrollBars = System.Windows.Forms.ScrollBars.Both; this.txtQidai.Size = new System.Drawing.Size(120, 42); this.txtQidai.TabIndex = 12; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(26, 7); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(29, 12); this.label1.TabIndex = 0; this.label1.Text = "Row:"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(8, 38); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(47, 12); this.label2.TabIndex = 0; this.label2.Text = "Column:"; // // txtRow // this.txtRow.Location = new System.Drawing.Point(74, 3); this.txtRow.Name = "txtRow"; this.txtRow.Size = new System.Drawing.Size(52, 21); this.txtRow.TabIndex = 3; this.txtRow.Leave += new System.EventHandler(this.txtRow_Leave); // // txtColumn // this.txtColumn.Location = new System.Drawing.Point(74, 34); this.txtColumn.Name = "txtColumn"; this.txtColumn.Size = new System.Drawing.Size(52, 21); this.txtColumn.TabIndex = 4; // // Commit // this.Commit.Location = new System.Drawing.Point(240, 253); this.Commit.Name = "Commit"; this.Commit.Size = new System.Drawing.Size(75, 23); this.Commit.TabIndex = 14; this.Commit.Text = "Commit"; this.Commit.UseVisualStyleBackColor = true; this.Commit.Click += new System.EventHandler(this.Commit_Click); // // cmbResult // this.cmbResult.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbResult.FormattingEnabled = true; this.cmbResult.ImeMode = System.Windows.Forms.ImeMode.Off; this.cmbResult.Items.AddRange(new object[] { "-", "DUBEG", "OK", "未确认"}); this.cmbResult.Location = new System.Drawing.Point(240, 226); this.cmbResult.Name = "cmbResult"; this.cmbResult.Size = new System.Drawing.Size(121, 20); this.cmbResult.TabIndex = 13; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(195, 230); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(35, 12); this.label3.TabIndex = 0; this.label3.Text = "结果:"; // // groupChoose // this.groupChoose.Controls.Add(this.txtColumn2); this.groupChoose.Controls.Add(this.txtRow2); this.groupChoose.Controls.Add(this.label10); this.groupChoose.Controls.Add(this.label9); this.groupChoose.Controls.Add(this.txtClass); this.groupChoose.Controls.Add(this.label8); this.groupChoose.Controls.Add(this.txtSheetName); this.groupChoose.Controls.Add(this.label7); this.groupChoose.Controls.Add(this.btnVisible); this.groupChoose.Controls.Add(this.btnColumn); this.groupChoose.Controls.Add(this.txtColumn1); this.groupChoose.Controls.Add(this.txtRow1); this.groupChoose.Controls.Add(this.label6); this.groupChoose.Controls.Add(this.label5); this.groupChoose.Controls.Add(this.btnOpen); this.groupChoose.Controls.Add(this.txtFileName); this.groupChoose.Controls.Add(this.label4); this.groupChoose.Location = new System.Drawing.Point(197, 130); this.groupChoose.Name = "groupChoose"; this.groupChoose.Size = new System.Drawing.Size(364, 285); this.groupChoose.TabIndex = 0; this.groupChoose.TabStop = false; this.groupChoose.Text = "Choose File"; // // txtColumn2 // this.txtColumn2.Location = new System.Drawing.Point(246, 138); this.txtColumn2.Name = "txtColumn2"; this.txtColumn2.Size = new System.Drawing.Size(40, 21); this.txtColumn2.TabIndex = 7; // // txtRow2 // this.txtRow2.Location = new System.Drawing.Point(103, 138); this.txtRow2.Name = "txtRow2"; this.txtRow2.Size = new System.Drawing.Size(40, 21); this.txtRow2.TabIndex = 6; // // label10 // this.label10.AutoSize = true; this.label10.Location = new System.Drawing.Point(162, 142); this.label10.Name = "label10"; this.label10.Size = new System.Drawing.Size(77, 12); this.label10.TabIndex = 0; this.label10.Text = "Info Column:"; // // label9 // this.label9.AutoSize = true; this.label9.Location = new System.Drawing.Point(31, 142); this.label9.Name = "label9"; this.label9.Size = new System.Drawing.Size(59, 12); this.label9.TabIndex = 0; this.label9.Text = "Info Row:"; // // txtClass // this.txtClass.Location = new System.Drawing.Point(103, 205); this.txtClass.Multiline = true; this.txtClass.Name = "txtClass"; this.txtClass.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.txtClass.Size = new System.Drawing.Size(100, 42); this.txtClass.TabIndex = 9; // // label8 // this.label8.AutoSize = true; this.label8.Location = new System.Drawing.Point(25, 220); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(65, 12); this.label8.TabIndex = 0; this.label8.Text = "TestClass:"; // // txtSheetName // this.txtSheetName.Location = new System.Drawing.Point(103, 175); this.txtSheetName.Name = "txtSheetName"; this.txtSheetName.Size = new System.Drawing.Size(100, 21); this.txtSheetName.TabIndex = 8; // // label7 // this.label7.AutoSize = true; this.label7.Location = new System.Drawing.Point(25, 179); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(65, 12); this.label7.TabIndex = 0; this.label7.Text = "SheetName:"; // // btnVisible // this.btnVisible.Location = new System.Drawing.Point(255, 233); this.btnVisible.Name = "btnVisible"; this.btnVisible.Size = new System.Drawing.Size(75, 23); this.btnVisible.TabIndex = 11; this.btnVisible.Text = "Visible"; this.btnVisible.UseVisualStyleBackColor = true; this.btnVisible.Click += new System.EventHandler(this.btnVisible_Click); // // btnColumn // this.btnColumn.Location = new System.Drawing.Point(255, 178); this.btnColumn.Name = "btnColumn"; this.btnColumn.Size = new System.Drawing.Size(75, 23); this.btnColumn.TabIndex = 10; this.btnColumn.Text = "OK"; this.btnColumn.UseVisualStyleBackColor = true; this.btnColumn.Click += new System.EventHandler(this.btnColumn_Click); // // txtColumn1 // this.txtColumn1.Location = new System.Drawing.Point(246, 104); this.txtColumn1.Name = "txtColumn1"; this.txtColumn1.Size = new System.Drawing.Size(40, 21); this.txtColumn1.TabIndex = 5; // // txtRow1 // this.txtRow1.Location = new System.Drawing.Point(103, 104); this.txtRow1.Name = "txtRow1"; this.txtRow1.Size = new System.Drawing.Size(40, 21); this.txtRow1.TabIndex = 4; // // label6 // this.label6.AutoSize = true; this.label6.Location = new System.Drawing.Point(150, 108); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(89, 12); this.label6.TabIndex = 0; this.label6.Text = "Result Column:"; // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(19, 108); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(71, 12); this.label5.TabIndex = 0; this.label5.Text = "Result Row:"; // // btnOpen // this.btnOpen.Location = new System.Drawing.Point(255, 27); this.btnOpen.Name = "btnOpen"; this.btnOpen.Size = new System.Drawing.Size(75, 23); this.btnOpen.TabIndex = 3; this.btnOpen.Text = "Open"; this.btnOpen.UseVisualStyleBackColor = true; this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click); // // txtFileName // this.txtFileName.Location = new System.Drawing.Point(30, 51); this.txtFileName.Multiline = true; this.txtFileName.Name = "txtFileName"; this.txtFileName.Size = new System.Drawing.Size(300, 42); this.txtFileName.TabIndex = 1; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(28, 33); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(65, 12); this.label4.TabIndex = 0; this.label4.Text = "File Path:"; // // lbRow1 // this.lbRow1.AutoSize = true; this.lbRow1.Location = new System.Drawing.Point(179, 7); this.lbRow1.Name = "lbRow1"; this.lbRow1.Size = new System.Drawing.Size(59, 12); this.lbRow1.TabIndex = 0; this.lbRow1.Text = "ResultRow"; // // lbColumn // this.lbColumn.AutoSize = true; this.lbColumn.Location = new System.Drawing.Point(179, 38); this.lbColumn.Name = "lbColumn"; this.lbColumn.Size = new System.Drawing.Size(77, 12); this.lbColumn.TabIndex = 0; this.lbColumn.Text = "ResultColumn"; // // panelResult // this.panelResult.Controls.Add(this.label1); this.panelResult.Controls.Add(this.groupChoose); this.panelResult.Controls.Add(this.lbColumn); this.panelResult.Controls.Add(this.checkHuamian); this.panelResult.Controls.Add(this.lbRow1); this.panelResult.Controls.Add(this.checkBupin); this.panelResult.Controls.Add(this.checkBanben); this.panelResult.Controls.Add(this.label3); this.panelResult.Controls.Add(this.checkModel); this.panelResult.Controls.Add(this.cmbResult); this.panelResult.Controls.Add(this.checkQianti); this.panelResult.Controls.Add(this.Commit); this.panelResult.Controls.Add(this.checkQidai); this.panelResult.Controls.Add(this.txtColumn); this.panelResult.Controls.Add(this.checkShoushun); this.panelResult.Controls.Add(this.txtRow); this.panelResult.Controls.Add(this.checkXian); this.panelResult.Controls.Add(this.label2); this.panelResult.Controls.Add(this.txtBanben); this.panelResult.Controls.Add(this.txtModel); this.panelResult.Controls.Add(this.txtQidai); this.panelResult.Controls.Add(this.txtHuamian); this.panelResult.Controls.Add(this.txtXian); this.panelResult.Controls.Add(this.txtBupin); this.panelResult.Controls.Add(this.txtShoushun); this.panelResult.Controls.Add(this.txtQianti); this.panelResult.Location = new System.Drawing.Point(12, 33); this.panelResult.Name = "panelResult"; this.panelResult.Size = new System.Drawing.Size(364, 285); this.panelResult.TabIndex = 27; // // toolStrip1 // this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripButton1, this.Save}); this.toolStrip1.Location = new System.Drawing.Point(0, 0); this.toolStrip1.Name = "toolStrip1"; this.toolStrip1.Size = new System.Drawing.Size(368, 25); this.toolStrip1.TabIndex = 28; this.toolStrip1.Text = "toolStrip1"; // // toolStripButton1 // this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; this.toolStripButton1.Enabled = false; this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta; this.toolStripButton1.Name = "toolStripButton1"; this.toolStripButton1.Size = new System.Drawing.Size(56, 22); this.toolStripButton1.Text = "Change"; this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton1_Click); // // Save // this.Save.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text; this.Save.Image = ((System.Drawing.Image)(resources.GetObject("Save.Image"))); this.Save.ImageTransparentColor = System.Drawing.Color.Magenta; this.Save.Name = "Save"; this.Save.Size = new System.Drawing.Size(39, 22); this.Save.Text = "Save"; this.Save.Click += new System.EventHandler(this.Save_Click); // // timer1 // this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(368, 311); this.Controls.Add(this.toolStrip1); this.Controls.Add(this.panelResult); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.MaximizeBox = false; this.Name = "Form1"; this.groupChoose.ResumeLayout(false); this.groupChoose.PerformLayout(); this.panelResult.ResumeLayout(false); this.panelResult.PerformLayout(); this.toolStrip1.ResumeLayout(false); this.toolStrip1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.CheckBox checkHuamian; private System.Windows.Forms.CheckBox checkBupin; private System.Windows.Forms.CheckBox checkBanben; private System.Windows.Forms.CheckBox checkModel; private System.Windows.Forms.CheckBox checkQianti; private System.Windows.Forms.CheckBox checkQidai; private System.Windows.Forms.CheckBox checkShoushun; private System.Windows.Forms.CheckBox checkXian; private System.Windows.Forms.TextBox txtBanben; private System.Windows.Forms.TextBox txtModel; private System.Windows.Forms.TextBox txtHuamian; private System.Windows.Forms.TextBox txtBupin; private System.Windows.Forms.TextBox txtQianti; private System.Windows.Forms.TextBox txtShoushun; private System.Windows.Forms.TextBox txtXian; private System.Windows.Forms.TextBox txtQidai; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox txtRow; private System.Windows.Forms.TextBox txtColumn; private System.Windows.Forms.Button Commit; private System.Windows.Forms.ComboBox cmbResult; private System.Windows.Forms.Label label3; private System.Windows.Forms.GroupBox groupChoose; private System.Windows.Forms.Button btnColumn; private System.Windows.Forms.TextBox txtColumn1; private System.Windows.Forms.TextBox txtRow1; private System.Windows.Forms.Label label5; private System.Windows.Forms.Button btnOpen; private System.Windows.Forms.TextBox txtFileName; private System.Windows.Forms.Label label4; private System.Windows.Forms.Button btnVisible; private System.Windows.Forms.Label lbRow1; private System.Windows.Forms.Label lbColumn; private System.Windows.Forms.Panel panelResult; private System.Windows.Forms.TextBox txtSheetName; private System.Windows.Forms.Label label7; private System.Windows.Forms.Label label6; private System.Windows.Forms.TextBox txtClass; private System.Windows.Forms.Label label8; private System.Windows.Forms.TextBox txtColumn2; private System.Windows.Forms.TextBox txtRow2; private System.Windows.Forms.Label label10; private System.Windows.Forms.Label label9; private System.Windows.Forms.ToolStrip toolStrip1; private System.Windows.Forms.ToolStripButton toolStripButton1; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.ToolStripButton Save; } }
/// <summary>
/// 自动调整列宽
/// </summary>
/// <param name="columnNum">列号</param>
public void ColumnAutoFit(int columnNum)
{
string strcolumnNum = GetColumnName(columnNum);
//获取当前正在使用的工作表
Excel.Worksheet worksheet = (Excel.Worksheet)myExcel.ActiveSheet;
Excel.Range range = (Excel.Range)worksheet.Columns[strcolumnNum + ":" + strcolumnNum, System.Type.Missing];
range.EntireColumn.AutoFit();
}
/// <summary>
/// 自动调整行高
/// </summary>
/// <param name="columnNum">列号</param>
public void RowAutoFit(int rowNum)
{
//获取当前正在使用的工作表
Excel.Worksheet worksheet = (Excel.Worksheet)myExcel.ActiveSheet;
Excel.Range range = (Excel.Range)worksheet.Rows[rowNum.ToString() + ":" + rowNum.ToString(), System.Type.Missing];
range.EntireColumn.AutoFit();
}http://blog.163.com/wei_zhiwei/blog/static/13036965620106931458630/
百度文库收藏
工程名WriteExcelF,添加Excel引用
在工程目录下新建一个TestResult.xml文件
命名空间要一致,tool工具要有图片