import com.jniwrapper.win32.automation.types.Variant; import com.jniwrapper.win32.com.ComException; import com.jniwrapper.win32.excel._Worksheet; import com.jniwrapper.win32.jexcel.ExcelException; import com.jniwrapper.win32.jexcel.Range; import com.jniwrapper.win32.jexcel.Worksheet; import com.jniwrapper.win32.jexcel.ui.JWorkbook;
import javax.swing.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent;
/** * This sample demonstrates how to protect rows and columns. * Sample requires jexcel-full.jar in classpath */ public class ColumnRowProtection { public static void main(String[] args) throws Exception { final JWorkbook _workbook = new JWorkbook(); Worksheet worksheet = null; try { worksheet = _workbook.addWorksheet("probe"); } catch (ExcelException e) { e.printStackTrace(); }
final Worksheet sheet = worksheet; final Range range = sheet.getRange("A:B");
final JFrame frame = new JFrame(); frame.setContentPane(_workbook); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { if (_workbook != null) { _workbook.close(); } } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800, 800); frame.setVisible(true);
_workbook.getApplication().getOleMessageLoop().doInvokeLater(new Runnable() { public void run() { try { _Worksheet _worksheet = sheet.getPeer(); _worksheet.getCells().setLocked(new Variant(true));//protect all cells on the worksheet range.getPeer().setLocked(new Variant(false));//the range will remain editable Variant missing = Variant.createUnspecifiedParameter(); _worksheet.protect( new Variant("password"), missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing ); } catch (ComException e) { e.printStackTrace(); } } }); } }
|