a.xls
sheet1
sheet2
sheet3
sheet3
印刷タイトル
・タイトル行[$1:$1]
sheet3の「A1」に「abc」と記入。
shee2を削除した時、sheet3の「印刷タイトル」の設定が消えなければ良いのですよね。
下記の小生のコードでは、消えませんでした。
(もともとXSS用をHSSに変更してあります。)
class LastSheetxException extends Exception{
public LastSheetxException (String msg) {super(msg);}
}
public class Class_excelx {
/**
* 「シート」を削除する。
* @param file_name Excelファイル名
* @param sheet_name シート名
* @param last_delete LastSheet削除許可
* @throws IOException
* @throws NonSheetException
* @throws LastSheetException
* @throws BiffException
* @throws WriteException
* @throws NoSheetException
*/
public void remove_sheet(String file_name, String sheet_name, boolean last_delete) throws IOException, LastSheetxException {
// Excelブックを開く
FileInputStream fi = new FileInputStream(file_name);
HSSFWorkbook hssfWorkbook =new HSSFWorkbook(fi);
fi.close();
// Sheet数の確認
if ((hssfWorkbook.getNumberOfSheets() == 1) && !last_delete) {
fi.close();
last_sheetx_exception(sheet_name);
}
// Sheetの削除
hssfWorkbook.removeSheetAt(hssfWorkbook.getSheetIndex(sheet_name));
// Excelブックの書き出し
OutputStream outStream = null;
try {
outStream = new BufferedOutputStream(new FileOutputStream(file_name));
hssfWorkbook.write(outStream);
} catch (Exception e) {
log.error(e.toString());
} finally {
try {
outStream.close();
} catch (Exception e1) {
log.error(e1.toString());
}
}
}
}
import java.io.IOException;
public class Xlsx {
public static void main(String[] args) {
Class_excelx ce = new Class_excelx();
try {
ce.remove_sheet("C:\\a.xls","sheet2",false);
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
} catch (LastSheetxException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/08 01:45
退会済みユーザー
2017/03/08 02:35