Lines 32-37
Link Here
|
32 |
|
32 |
|
33 |
import org.apache.poi.POIXMLDocumentPart; |
33 |
import org.apache.poi.POIXMLDocumentPart; |
34 |
import org.apache.poi.POIXMLException; |
34 |
import org.apache.poi.POIXMLException; |
|
|
35 |
import org.apache.poi.hssf.record.PasswordRecord; |
35 |
import org.apache.poi.hssf.record.formula.FormulaShifter; |
36 |
import org.apache.poi.hssf.record.formula.FormulaShifter; |
36 |
import org.apache.poi.hssf.util.PaneInformation; |
37 |
import org.apache.poi.hssf.util.PaneInformation; |
37 |
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
38 |
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
Lines 52-57
Link Here
|
52 |
import org.apache.poi.ss.util.CellRangeAddressList; |
53 |
import org.apache.poi.ss.util.CellRangeAddressList; |
53 |
import org.apache.poi.ss.util.CellReference; |
54 |
import org.apache.poi.ss.util.CellReference; |
54 |
import org.apache.poi.ss.util.SSCellRange; |
55 |
import org.apache.poi.ss.util.SSCellRange; |
|
|
56 |
import org.apache.poi.util.HexDump; |
55 |
import org.apache.poi.util.Internal; |
57 |
import org.apache.poi.util.Internal; |
56 |
import org.apache.poi.util.POILogFactory; |
58 |
import org.apache.poi.util.POILogFactory; |
57 |
import org.apache.poi.util.POILogger; |
59 |
import org.apache.poi.util.POILogger; |
Lines 941-947
Link Here
|
941 |
public boolean getProtect() { |
943 |
public boolean getProtect() { |
942 |
return worksheet.isSetSheetProtection() && sheetProtectionEnabled(); |
944 |
return worksheet.isSetSheetProtection() && sheetProtectionEnabled(); |
943 |
} |
945 |
} |
|
|
946 |
|
947 |
/** |
948 |
* Enables sheet protection and sets the password for the sheet. |
949 |
* Also sets some attributes on the {@link CTSheetProtection} that correspond to |
950 |
* the default values used by Excel |
951 |
* |
952 |
* @param password to set for protection. Pass <code>null</code> to remove protection |
953 |
*/ |
954 |
@Override |
955 |
public void protectSheet(String password) { |
956 |
|
957 |
if(password != null) { |
958 |
CTSheetProtection sheetProtection = worksheet.addNewSheetProtection(); |
959 |
sheetProtection.xsetPassword(stringToExcelPassword(password)); |
960 |
sheetProtection.setSheet(true); |
961 |
sheetProtection.setScenarios(true); |
962 |
sheetProtection.setObjects(true); |
963 |
} else { |
964 |
worksheet.unsetSheetProtection(); |
965 |
} |
966 |
} |
944 |
|
967 |
|
|
|
968 |
/** |
969 |
* Converts a String to a {@link STUnsignedShortHex} value that contains the {@link PasswordRecord#hashPassword(String)} |
970 |
* value in hexadecimal format |
971 |
* |
972 |
* @param password the password string you wish convert to an {@link STUnsignedShortHex} |
973 |
* @return {@link STUnsignedShortHex} that contains Excel hashed password in Hex format |
974 |
*/ |
975 |
private STUnsignedShortHex stringToExcelPassword(String password) { |
976 |
STUnsignedShortHex hexPassword = STUnsignedShortHex.Factory.newInstance(); |
977 |
hexPassword.setStringValue(String.valueOf(HexDump.shortToHex(PasswordRecord.hashPassword(password))).substring(2)); |
978 |
return hexPassword; |
979 |
} |
980 |
|
945 |
/** |
981 |
/** |
946 |
* Returns the logical row ( 0-based). If you ask for a row that is not |
982 |
* Returns the logical row ( 0-based). If you ask for a row that is not |
947 |
* defined you get a null. This is to say row 4 represents the fifth row on a sheet. |
983 |
* defined you get a null. This is to say row 4 represents the fifth row on a sheet. |