excel C#.txt

Embed Size (px)

DESCRIPTION

vgjhj

Citation preview

01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 ; 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59namespace WindowsFormsApplication1 { partial class Form1 { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// public void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(12, 97); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "Write"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(button1_Click) // // textBox1 // this.textBox1.Location = new System.Drawing.Point(12, 61); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 20); this.textBox1.TabIndex = 1; this.textBox1.Text = ""; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(12, 35); this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(100, 20); this.textBox2.TabIndex = 2; this.textBox1.Text = ""; // // Form1 //60 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F) ; 61 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font ; 62 this.ClientSize = new System.Drawing.Size(292, 266); 63 this.Controls.Add(this.textBox2); 64 this.Controls.Add(this.textBox1); 65 this.Controls.Add(this.button1); 66 this.Name = "Form1"; 67 this.Text = "Form1"; 68 this.ResumeLayout(false); 69 this.PerformLayout(); 70 71 } 72 73 public void button1_Click(object sender, System.EventArgs e) 74 { 75 Form1 form = new Form1(); 76 MyExcel excel = new MyExcel(); 77 78 string one = form.textBox1.Text; 79 string two = form.textBox2.Text; 80 81 excel.ExcelApp(one, two); 82 } 83 84 #endregion 85 86 public System.Windows.Forms.Button button1; 87 public System.Windows.Forms.TextBox textBox1; 88 public System.Windows.Forms.TextBox textBox2; 89 } 90 } xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 001 using System; 002 using System.Collections.Generic; 003 using System.Linq; 004 using System.Text; 005 using System.Windows; 006 using System.Windows.Controls; 007 using System.Windows.Data; 008 using System.Windows.Documents; 009 using System.Windows.Input; 010 using System.Windows.Media; 011 using System.Windows.Media.Imaging; 012 using System.Windows.Navigation; 013 using System.Windows.Shapes; 014 using System.Globalization; 015 using Excel = Microsoft.Office.Interop.Excel; 016 using System.Reflection; 017 018 019 namespace PlantDataTools 020 { 021 /// 022 /// Interaction logic for Window1.xaml 023 /// 024 public partial class Window1 : Window 025 {026 public Window1() 027 { 028 InitializeComponent(); 029 } 030 031 private void button1_Click(object sender, RoutedEventArgs e) 032 { 033 DateTime? startDate; 034 DateTime? endDate; 035 036 if (!IsValidDate(startDateTextBox)) 037 { 038 ErrorMessageLabel.Visibility = Visibility.Visible; 039 ErrorMessageLabel.Content = "Invalid Start Date."; 040 startDate = null; 041 } 042 else 043 startDate = Convert.ToDateTime(startDateTextBox.Text); 044 if (!IsValidDate(EndDateTextBox)) 045 { 046 ErrorMessageLabel.Visibility = Visibility.Visible; 047 ErrorMessageLabel.Content = "Invalid End Date."; 048 endDate = null; 049 } 050 else 051 endDate = Convert.ToDateTime(EndDateTextBox.Text); 052 053 if (startDate != null && endDate != null) 054 { 055 GetData((DateTime)startDate, (DateTime)endDate); 056 } 057 } 058 059 060 061 private bool IsValidDate(TextBox textBox) 062 { 063 if (!string.IsNullOrEmpty(textBox.Text)) 064 { 065 string[] formats = { "M/d/yyyy", "M/d/yy" }; 066 DateTime value; 067 068 if (!DateTime.TryParseExact(textBox.Text, formats, new C ultureInfo("en-US"), DateTimeStyles.None, out value)) 069 { 070 return false; 071 } 072 } 073 return true; 074 } // --- END IsVaidDate --075 076 private void GetData(DateTime startTime, DateTime endTime) 077 { 078 079 //Declare Excel Objects... 080 Excel.Application xlApplication; 081 Excel._Workbook xlWorkbook; 082 Excel._Workbook xlDataWorkbook; 083 Excel._Worksheet xlWorksheet; 084 Excel._Worksheet xlDataWorksheet;085 Excel.Range xlWorkingRange; 086 Excel.Range xlDataRange; 087 object missing = Missing.Value; 088 089 //Declare method variables... 090 decimal grossGeneration; 091 decimal netGeneration; 092 System.Array myValues; 093 decimal[] decArray; 094 095 //Set currentDate to startDate. 096 DateTime currentDate = startTime; 097 098 int totalDays = (endTime - startTime).Days + 1; 099 100 ErrorMessageLabel.Content = totalDays.ToString(); 101 ErrorMessageLabel.Visibility = Visibility.Visible; 102 103 try 104 { 105 //Start Excel and get Application Object. 106 xlApplication = new Excel.Application(); 107 xlApplication.Visible = true; 108 109 xlWorkbook = (Excel._Workbook)(xlApplication.Workbooks.A dd(missing)); 110 xlWorksheet = (Excel._Worksheet)xlWorkbook.Worksheets["s heet1"]; 111 xlWorksheet.Cells[1, 1] = "Date"; 112 xlWorksheet.Cells[1, 2] = "U1 CT Gross"; 113 xlWorksheet.Cells[1, 3] = "U1 ST Gross"; 114 xlWorksheet.Cells[1, 4] = "U2 CT Gross"; 115 xlWorksheet.Cells[1, 5] = "U2 ST Gross"; 116 xlWorksheet.Cells[1, 6] = "U3 CT Gross"; 117 xlWorksheet.Cells[1, 7] = "U3 ST Gross"; 118 xlWorksheet.Cells[1, 8] = "Line 1 Net"; 119 xlWorksheet.Cells[1, 9] = "Line 2 Net"; 120 xlWorksheet.Cells[1, 10] = "Gross Output"; 121 xlWorksheet.Cells[1, 11] = "Net Output"; 122 123 for (int i = 1; i Hi > > Can anyone help me how to read values in Excel in C# . So that Once I > read I can send them to DataBase. My excel file Test.xls in ("C:\") > > Thanks > Doss > 64 Answers Found Answer 1 Hi Can anyone help me how to read values in excel in C# . So that Once I read I can send them to DataBase. My excel file Test.xls in ("C:\") Thanks DossAnswer 2 I would guess: open Excel from your program, and have Excel read the data. Search for Visual Studio Tools for Office (VSTO) for the details. Answer 3 His Doss, Create a reference in your project to excel Objects Library. The exc el object library can be added in the COM tab of adding reference dialog. I hope the following code in your menu click event method will help you a lot to achi eve your need. this.openFileDialog1.FileName = "*.xls"; if (this.openFileDialog1.ShowDialog() == DialogResult.OK) { Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open( openFileDialog1.FileName, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true); Excel.Sheets sheets = theWorkbook.Worksheets; Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1); for (int i = 1; i //definition of worksheet is Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1); int stemp = Convert.ToInt32 ( worksheet.Cells[1, 1]); Regards Harshal Answer 31 Hi, did you find out a way to dynamically insert a button in excel and get your c# method to be called when the button is clicked? Answer 32Hello, I will refer here only the Web Applications. It is not possible to perform file I / O operations at Web Applications. Although you can read Excel files on .csv / .txt format. You can find an example on my site: http://www.wwv-it.com/WebServicesNet/ServicesNet.aspx, category Data conversions ,Service "Convert Excel / Text files to data Sets / XML files / database tables ". I would be glad if you could help with this example. Do not hesitate to contact me if you need more information or code. Regards EinsteinAnswer 33 j Answer 34 so Answer 35 so Answer 36 Dear fellows I have problem about which i donot from where i can get help. What is Working: I have made a ASP.NET (C#) application that is reading Data from excel (cell bcell). That is working fine and i can check it in browser. Problem: Now using this code i made a web part in C# and i want to show it in SharPoint S erver 2007 site page. Now that is not working , thats mean not showing the web part on the page. And when i comment the excel ralated code in web part coding and check it at Sha rePoint server, web part works. I donot know what to do, Please Help Piece of code is: Excel.Application app = new Excel.Application(); Excel.Workbook wbook = null; Excel.Worksheet wsheet = null; Excel.Range range = null; app.Visible = false; Thread.CurrentThread.CurrentCulture = newCultureInfo("en-US"); string filepath = inputFile1.Value.ToString(); if (filepath != "") { wbook = app.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, Missing.Value, Missing.Value); string currentSheet = "Sheet1"; wsheet = (Excel.Worksheet)wbook.Worksheets.get_Item(currentSheet); range = wsheet.get_Range("B6", "H20"); System.Array myvalues = (System.Array)range.Cells.Value2; valueArray = ConvertToStringArray(myvalues);if (app != null) { app.Workbooks.Close(); app.Quit(); } app = null; wsheet = null; range = null; Answer 37 Hi. To use this I need to add the microsoft Excel refrence to the project, right? The thing is I dont have microsoft offince on my computer. I use open office whi ch is kind of the same thing. But the problem is that I dont have that refrence in the com that when I want to add the refrence to my project. Is there any oth er way to do this or anywhere I can get that reference thing? thx Answer 38 Does anybody know to figure out what range of data was used for a chart object?I like this bit of code posted earlier because it is easy to use: Range myRange = (Range) sheets.Cells[30,10]; currentRate = (float)Convert.ToDouble(myRange.Cells.Value2.ToString()); myRange = (Range)sheets.Cells[30, 2];So, my plan is to figure out which cells were used to create a particular chart - i.e. which cells are the x-axis and which cells are the y-axis. Then just loo p through and grab the values to an array.Any thought would be great...Answer 39 learning Answer 40 I cannot find the function ConvertToStringArray. What dll do I need to include in my project to be able to use this function?Answer 41 This is not ready made function, you have to write it yourself, here i can give u example, you can ajust the lengths acooring to your need, string[] ConvertToStringArray(System.Array values) { // create a new string array string[] theArray = newstring[values.Length]; int position = 0; // loop through the 2-D System.Array and populate the 1-D String Array for (int i = 1; i