Lm th? no ? t?o/?nh d?ng m?t b?ng tnh Excel s? d?ng tr?c quan J ++ t? ?ng ha

D?ch tiu ? D?ch tiu ?
ID c?a bi: 219430 - Xem s?n ph?m m bi ny p d?ng vo.
Bung t?t c? | Thu g?n t?t c?


Bi vi?t ny m t? Automating Excel 97 tr? ln t? m?t Th? gic J ++ khch b?ng cch s? d?ng Java ngn ng? l?p tr?nh. D? n s? d?ng cc Excel ki?u th vi?n, v minh ho? l?p tr?nh h?ng ?i t?ng.


Bi vi?t ny song m?t bi vi?t c s? ki?n th?c Microsoft m t? qu tr?nh b?ng cch s? d?ng Microsoft Visual C++ v Microsoft Foundation Cc l?p h?c. ? bi?t thm chi ti?t v? t? ?ng ha c?a Excel t? Visual C++, nh?p vo s? bi vi?t sau ? xem bi vi?t trong c s? ki?n th?c Microsoft:
179706Lm th? no ? s? d?ng MFC ? t? ?ng ho Excel v t?o/?nh d?ng m?t Workbook m?i
  1. B?t ?u Visual J C++ 6.0. T?o ra m?t ?ng d?ng m?i giao di?n i?u khi?n d? n v ?t tn l XLJava1.
  2. Trong c?a s? d? n-Explorer, m? cy d? n c?a b?n v b?m p vo t?p Class1.java ? ?c t?o ra cho b?n.
  3. T? cc D an tr?nh n, ch?n Thm COM Wrapper, ch?n cc Microsoft Excel 8,0, 9,0 ho?c 10,0 (nh thch h?p) ?i t?ng th vi?n, v sau nh?p vo Ok. i?u ny cho bi?t thm hm bao COM Java c ngu?n g?c t? lo?i Excel th vi?n ? d? n c?a b?n.
  4. ? ?u t?p tin c?a b?n Class1.Java, thm d?i y chuy?n nh?p pht bi?u:
       import Excel8.*; // Change the 8 to 9 for Excel 2000, or 10 for 2002 or later
       // For Excel 2000 the statement should read
       // import Excel9.*; // Excel support
       import com.ms.com.*; // Variant & exception support. 
       import java.lang.InterruptedException; // Needed for Thread.sleep().
       (These are illustrated in the code sample below.)
  5. Trong Class1 i?m v ch?c nng main(), thm m? ? cc chng tr?nh c?a b?n s? xu?t hi?n nh sau:
     * This class can take a variable number of parameters on the command
     * line. Program execution begins with the main() method. The class
     * constructor is not invoked unless an object of type 'Class1'
     * created in the main() method.
    import excel.*; // for Excel 2002, or - import excel9.*; for Excel 2000 or later
                      // or - import excel8.*; for Excel 97
    import java.lang.InterruptedException; // needed for thread.sleep
    import com.ms.com.*; // Variant & exception support<BR/>
    public class Class1
    	 * The main entry point for the application. 
    	 * @param args Array of parameters passed to the application
    	 * via the command line.
    	// Add the following static member function declaration 
        // just before main()...
        // J/Direct declarations... to use Windows MessageBox
        /** @dll.import("USER32") */ 
        private static native int MessageBox(int hwndOwner, String text, 
                                             String title, int fuStyle);
        // Here is an example of its use.
        // MessageBox(0, "Hello, World", "Title", 0); //fuStyle switch
        //    values are defined in Winuser.h. Search on 'MB_OK'
    	public static void main (String[] args)
    		// Force COM objects to be created on the current thread.
          // Otherwise, older VMs might not release all references
          // and Excel might continue to run after you shutdown.
          // Launch Excel 
          Variant vEmpty = new Variant();
          Application xlApp = new Application();
          //xlApp.setVisible(0,true); // Excel 2000 & 97 1st param is LCID, Locale ID
    	  xlApp.setVisible(true); // One argument for Excel 2002 or later
          // Get the workbook object via the object model.
          Workbooks books = xlApp.getWorkbooks();
          _Workbook book = books.Add(vEmpty,0);
          // Get the first sheet.		
          Sheets sheets = (Sheets)book.getSheets();
          _Worksheet sheet = (_Worksheet)sheets.getItem(new Variant(1));
          // Fill cells A1, B1, C1, and D1, one cell at a time, with "headers".
          Range range = sheet.getRange(new Variant("A1"),new Variant("A1"));
          //range.setValue(new Variant("First Name")); // Throughout, the SetValue
          // of Excel 97 and Excel 2000 no longer works in Excel 2002 or later.
    	  range.setValue2(new Variant("First Name"));
          range = sheet.getRange(new Variant("B1"),new Variant("B1"));
          //range.setValue(new Variant("Last Name"));
    	  range.setValue2(new Variant("Last Name"));
          range = sheet.getRange(new Variant("C1"),new Variant("C1"));
          //range.setValue(new Variant("Full Name"));
    	  range.setValue2(new Variant("Full Name"));
          range = sheet.getRange(new Variant("D1"),new Variant("D1"));
          //range.setValue(new Variant("Salary"));
    	  range.setValue2(new Variant("Salary"));
          // Format A1:D1 as bold, vertical alignment = center
          range = sheet.getRange(new Variant("A1"),new Variant("D1"));
          Font font = range.getFont();
          font.setBold(new Variant(true));
          range.setVerticalAlignment(new Variant(-4108));
          // Fill range A2:B6 with first and last names, 
          range = sheet.getRange(new Variant("A2"),new Variant("A2"));
          //range.setValue(new Variant("John"));
    	  range.setValue2(new Variant("John"));
          range = sheet.getRange(new Variant("B2"),new Variant("B2"));
          //range.setValue(new Variant("Smith"));
    	  range.setValue2(new Variant("Smith"));
          range = sheet.getRange(new Variant("A3"),new Variant("A3"));
          //range.setValue(new Variant("Tom"));
    	  range.setValue2(new Variant("Tom"));
          range = sheet.getRange(new Variant("B3"),new Variant("B3"));
          //range.setValue(new Variant("Brown"));
    	  range.setValue2(new Variant("Brown"));
          range = sheet.getRange(new Variant("A4"),new Variant("A4"));
          //range.setValue(new Variant("Sue"));
    	  range.setValue2(new Variant("Sue"));
          range = sheet.getRange(new Variant("B4"),new Variant("B4"));
          //range.setValue(new Variant("Thomas"));
    	  range.setValue2(new Variant("Thomas"));
          range = sheet.getRange(new Variant("A5"),new Variant("A5"));
          //range.setValue(new Variant("Jane"));
    	  range.setValue2(new Variant("Jane"));
          range = sheet.getRange(new Variant("B5"),new Variant("B5"));
          //range.setValue(new Variant("Jones"));
    	  range.setValue2(new Variant("Jones"));
          range = sheet.getRange(new Variant("A6"),new Variant("A6"));
          //range.setValue(new Variant("Adam"));
    	  range.setValue2(new Variant("Adam"));
          range = sheet.getRange(new Variant("B6"),new Variant("B6"));
          //range.setValue(new Variant("Johnson"));
    	  range.setValue2(new Variant("Johnson"));
          // Fill range C2:C6 with a relative formula(=A2 & " " & B2")
          range = sheet.getRange(new Variant("C2"),new Variant("C6"));
          range.setFormula(new Variant("=A2 & \" \" & B2"));
          // Fill D2:D6 with a formula (=RAND()*100000) and apply a 
          //  number format
          range = sheet.getRange(new Variant("D2"),new Variant("D6"));
          range.setFormula(new Variant("=RAND()*100000"));
          range.setNumberFormat(new Variant("$0.00"));
          // AutoFit columns A:D
          range = sheet.getRange(new Variant("A1"),new Variant("D1"));
          Range columns = range.getEntireColumn();
          // Manipulate a variable number of columns
          //  for Quarterly Sales Data
          short NumQtrs;
          String msg;
          String msg2;
          msg2 = " "; // Initialize it to avoid compile error
          int reply;
          Range resizedrange;
          Interior interior;
          Borders borders;
          // Determine how many quarters to display data for.
          for(NumQtrs = 1; NumQtrs<=4; NumQtrs++)
             if (NumQtrs == 1)
              msg = "Enter sales data for " + NumQtrs + " quarter?";
              msg2 = "Displaying data for " + NumQtrs + " quarter";
              msg = "Enter sales data for " + NumQtrs + " quarters?";
              msg2 = "Displaying data for " + NumQtrs + " quarters";
             reply = MessageBox(0, msg, "Indicate Quarters", 36);
             if(6 == reply) break;			
          } // end For loop
          MessageBox(0, msg2, "Data Setting", 0);
          if(5 == NumQtrs) // Limit display to <=4 quarters
           NumQtrs = 4;
          // Starting at E1, fill headers for number of columns selected.
          range = sheet.getRange(new Variant("E1"),new Variant("E1"));
          resizedrange = range.getResize(vEmpty, new Variant(NumQtrs));
             new Variant("= \"Q\" & COLUMN()-4 & CHAR(10)& \"Sales\""));
          // Change the Orientation and Wrap Text properties for the headers.
          resizedrange.setOrientation(new Variant(38));
          resizedrange.setWrapText(new Variant(true));
          // Fill the interior colors of the headers.
          interior = resizedrange.getInterior();
          interior.setColorIndex(new Variant(36));
          // Fill the columns with a formula and apply a number format.
          range = sheet.getRange(new Variant("E2"),new Variant("E6"));
          resizedrange = range.getResize(vEmpty, new Variant(NumQtrs));
          resizedrange.setFormula(new Variant("=RAND()*100"));
          resizedrange.setNumberFormat(new Variant("$0.00"));
          // Apply Borders to the Sales data and headers
          range = sheet.getRange(new Variant("E1"), new Variant("E6"));
          resizedrange = range.getResize(vEmpty, new Variant(NumQtrs));
          borders = resizedrange.getBorders();
          borders.setWeight(new Variant(2)); // xlThin = 2
          // Add Totals formula for Quarterly sales data, apply a border.
          range = sheet.getRange(new Variant("E8"),new Variant("E8"));
          resizedrange = range.getResize(vEmpty, new Variant(NumQtrs));
          resizedrange.setFormula(new Variant("=SUM(E2:E6)"));
          borders = resizedrange.getBorders();
          Border bottomborder;
          bottomborder = borders.getItem(9); // Bottom border
          bottomborder.setLineStyle(new Variant(-4119)); // xlDouble = -4119
          bottomborder.setWeight(new Variant(4));  // xlThick = 4
          // Some more features: Add comment to cell C3 - Full 
          //    name of Tom Brown
          range = sheet.getRange(new Variant("C3"),new Variant("C3"));
          range.AddComment(new Variant("Tom was last month's top performer"));
          // Chart the Salaries.
          double Left = 72.; // location in points
          double Top = 144.;
          double Width = 350.;
          double Height = 250.;
          ChartObjects chartobjects = 
            (ChartObjects)sheet.ChartObjects(vEmpty,0); // explicit typecast
          ChartObject chartobject = chartobjects.Add(Left, Top, Width, Height);
          // Set the location and size.
          Chart chart = chartobject.getChart();
          range = sheet.getRange(new Variant("C2"), new Variant("D6"));
          // Full name and salary.
          chart.ChartWizard(new Variant(range), // Source
                            new Variant(11),    // Gallery
                            vEmpty,             // Format
                            new Variant(1),     // PlotBy
                            new Variant(0),     // CategoryLabels
                            new Variant(1),     // SeriesLabels
                            new Variant(true),  // HasLegend
                            new Variant("Salary by Employee"), //Title
                            new Variant("Employee"), //CategoryTitle
                            new Variant("Salary"), //ValueTitle
                            vEmpty,             // ExtraTitle
                            0                   // LCID(localeID)
          // Make the application visible and give the user control of 
          // Microsoft Excel.
          // xlApp.setVisible(0,true); // 1st param is LCID, Locale ID. 
          // The preceding was done at beginning of the program.
          MessageBox(0, "When Excel hides, Minimize VJ++ IDE to see Excel",
                     "Focus on Excel", 327744);
          //book.setSaved(0,true); // Avoid "Save changes" dialog
          //xlApp.Quit();  // Not if using UserControl		
    	} // End of Main()
    } // End of Class
  6. T? cc G? l?i tr?nh n, nh?p vo B?t ?u. Chng tr?nh ?c xy d?ng, gi, v ch?y. Khi n ch?y, bn i?u khi?n c?a s? s? xu?t hi?n v m?t s? th?i gian sau ny Excel xu?t hi?n. M?t h?p tho?i nh?c b?n v? lm th? no c th? khu vo b?ng x?p h?ng s? xu?t hi?n trn ?u trang tr? khi b?n b?m vo b?t c? ni no trn c?a b?n mn h?nh sau khi chng tr?nh b?t ?u. N?u b?n ? nh?p vo mn h?nh h?p tho?i s? ?c gi?m thi?u trn thanh tc v? Windows. N?u chng tr?nh s? xu?t hi?n ? treo, Khi ph?c 'B?nh th?ng' h?p trn thanh tc v?.


? c thm thng tin v? J Visual C++ v t? ?ng ha, nh?p vo s? bi vi?t sau ? xem cc bi vi?t trong c s? ki?n th?c Microsoft:
169173Thng tin: Cu h?i cho th? gic j C++
215484 Lm th? no ? t? ?ng ho PowerPoint s? d?ng + tr?c quan J +
179706 Lm th? no ? s? d?ng MFC ? t? ?ng ho Excel v t?o/?nh d?ng m?t Workbook m?i
219151 Lm th? no ? t? ?ng ho Microsoft Excel t? Visual Basic

Thu?c tnh

ID c?a bi: 219430 - L?n xem xt sau cng: 21 Thang Tam 2011 - Xem xt l?i: 2.0
p d?ng
  • Microsoft Visual J++ 6.0 Standard Edition
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Office XP Developer Edition
  • Microsoft Office 2000 Developer Edition
T? kha:
kbautomation kbhowto kbmt KB219430 KbMtvi
My d?ch
QUAN TRONG: Bi vi?t ny ?c d?ch b?ng ph?n m?m d?ch my c?a Microsoft ch? khng ph?i do con ng?i d?ch. Microsoft cung c?p cc bi vi?t do con ng?i d?ch v c? cc bi vi?t do my d?ch ? b?n c th? truy c?p vo t?t c? cc bi vi?t trong C s? Ki?n th?c c?a chng ti b?ng ngn ng? c?a b?n. Tuy nhin, bi vi?t do my d?ch khng ph?i lc no c?ng hon h?o. Lo?i bi vi?t ny c th? ch?a cc sai st v? t? v?ng, c php ho?c ng? php, gi?ng nh m?t ng?i n?c ngoi c th? m?c sai st khi ni ngn ng? c?a b?n. Microsoft khng ch?u trch nhi?m v? b?t k? s? thi?u chnh xc, sai st ho?c thi?t h?i no do vi?c d?ch sai n?i dung ho?c do ho?t ?ng s? d?ng c?a khch hng gy ra. Microsoft c?ng th?ng xuyn c?p nh?t ph?n m?m d?ch my ny.
Nh?p chu?t vo y ? xem b?n ti?ng Anh c?a bi vi?t ny:219430

Cung cp Phan hi


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com