home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1998 July & August
/
Pcwk78a98.iso
/
Micrsoft
/
SAMPLES
/
VJ6SAMPL.EXE
/
XlCellEdit
/
XlCellEdit.java
< prev
next >
Wrap
Text File
|
1998-02-24
|
6KB
|
179 lines
/* Copyright (C) Microsoft Corporation, 1996-1998. All rights reserved.
This source code is intended only as a supplement to Microsoft
Visual J++ 6.0. See this product's on-line documentation for detailed
information regarding Microsoft code samples.
*/
import wfc.app.*;
import wfc.core.*;
import wfc.ui.*;
import com.ms.com.Variant;
import excel8.Sheets;
import excel8._Worksheet;
import excel8.Workbooks;
import excel8._Workbook;
import excel8.Range;
import excel8._Application;
/**
* Before building and running this project, you must create
* wrappers for the Microsoft Excel 8.0 Object Library.
* To create the wrappers, select the 'Project' menu and
* choose the 'Add COM Wrapper...' item. When the COM Wrapper
* dialog appears, check the box for 'Microsoft Excel 8.0
* Object Library'. This item will only appear if Excel 8.0
* has already been installed on the system. Click 'OK' to
* close the dialog and create the wrappers.
*
* Three packages will be added to your project:
* _excel8........ Excel wrappers
* _mso97......... Office wrappers
* _vbeext1....... VBE wrappers
*
* Once the wrapper packages have been created, build and
* run the sample.
*/
public class XlCellEdit extends Form {
Range range;
_Application excel;
private void Form_Closed(Object sender, Event e) {
if (excel != null) {
excel.Quit();
}
}
private void setButton_Click(Object sender, Event e) {
Variant newString = new Variant();
newString.putString(edit.getText());
range.setValue(newString);
}
private void getButton_Click(Object sender, Event e){
Variant oldString = new Variant();
oldString = range.getValue();
edit.setText(oldString.toString());
}
private void xlButton_Click(Object sender, Event e) {
//----------------------------------
// Fire up Excel and make it visible
//----------------------------------
excel = (_Application) new excel8.Application();
excel.setVisible(0,true);
//-----------------------------
// Add a workbook and worksheet
//-----------------------------
Workbooks books = excel.getWorkbooks();
Variant one = new Variant();
one.putInt(1);
books.Add(one,0);
//---------------------------------------------------
// Select the first workbook (the one we just added)
//---------------------------------------------------
_Workbook book = (_Workbook) books.getItem(one);
//----------------------------------------------
// Select the first worksheet (the one we added)
//----------------------------------------------
Sheets sheets = book.getWorksheets();
_Worksheet sheet = (_Worksheet) sheets.getItem(one);
//-----------------------------------------------------
// Select a range of cells (A1,A1) and get their value
//-----------------------------------------------------
Variant rangeStart = new Variant();
Variant rangeEnd = new Variant();
Variant newString = new Variant();
Variant oldString = new Variant();
rangeStart.putString("A1");
rangeEnd.putString("A1");
newString.putString("Hello Office!");
range = sheet.getRange(rangeStart,rangeEnd);
range.setValue(newString);
oldString = range.getValue();
edit.setText(oldString.getString());
}
public XlCellEdit(){
//Required for Visual J++ Form Designer support
initForm();
}
/**
* The main entry point for the application.
*
* @param <args> Array of parameters passed to the application
* via the command line.
*/
public static void main(String args[]){
wfc.app.Application.run(new XlCellEdit());
}
/**
* NOTE: The following code is required by the Visual J++ form
* designer. It can be modified using the form editor. Do not
* modify it using the code editor.
*/
Container components = new Container();
Button xlButton = new Button();
Button getButton = new Button();
Button setButton = new Button();
Edit edit = new Edit();
Label label = new Label();
private void initForm()
{
this.setBackColor(Color.CONTROL);
this.setSize(new Point(297, 179));
this.setText("Excel 8.0 Sample");
this.setAutoScaleBaseSize(16);
this.setClientSize(new Point(289, 152));
this.addOnClosed(new EventHandler(this.Form_Closed));
xlButton.setLocation(new Point(30, 20));
xlButton.setSize(new Point(90, 20));
xlButton.setTabIndex(0);
xlButton.setTabStop(true);
xlButton.setText("&Launch Excel...");
xlButton.addOnClick(new EventHandler(this.xlButton_Click));
getButton.setLocation(new Point(210, 120));
getButton.setSize(new Point(70, 20));
getButton.setTabIndex(1);
getButton.setTabStop(true);
getButton.setText("&Get");
getButton.addOnClick(new EventHandler(this.getButton_Click));
setButton.setLocation(new Point(210, 90));
setButton.setSize(new Point(70, 20));
setButton.setTabIndex(2);
setButton.setTabStop(true);
setButton.setText("&Set");
setButton.addOnClick(new EventHandler(this.setButton_Click));
edit.setBackColor(Color.WINDOW);
edit.setCursor(Cursor.IBEAM);
edit.setLocation(new Point(30, 90));
edit.setSize(new Point(170, 23));
edit.setTabIndex(3);
edit.setTabStop(true);
edit.setText("(Empty)");
label.setLocation(new Point(30, 70));
label.setSize(new Point(170, 20));
label.setText("Cell Value (Column A, Row 1)");
label.setTabIndex(5);
this.setNewControls(new Control[] {
label,
edit,
setButton,
getButton,
xlButton});
}
//NOTE: End of form designer support code.
}