Compare commits

..

30 Commits

Author SHA1 Message Date
Cédric Walter fe865ca3c1 migrated javadoc settings from ant to maven plugin configuration
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1585845 13f79535-47bb-0310-9956-ffa450edef68
2014-04-08 21:51:23 +00:00
Cédric Walter d697a4860f cleaned up module name, use classifier (tests) for exporting test classes and resources to any module, remove empty directories
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1585837 13f79535-47bb-0310-9956-ffa450edef68
2014-04-08 21:27:10 +00:00
Cédric Walter 38e8f7e9af cleaned up pom.xml
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1584913 13f79535-47bb-0310-9956-ffa450edef68
2014-04-04 21:09:47 +00:00
Cédric Walter 6363c488c7 moved test resources of poi to module poi-main
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1584909 13f79535-47bb-0310-9956-ffa450edef68
2014-04-04 21:05:13 +00:00
Cédric Walter da8760b9aa removed maven install ant task as maven does this in mvn install
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1584040 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 14:07:44 +00:00
Cédric Walter 0de0c8b387 removed creation of maven pom in ant build as maven does this automatically when running package
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1584038 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 14:04:21 +00:00
Cédric Walter 2379629418 added source code to all artifacts
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1584035 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 13:57:10 +00:00
Cédric Walter c197b561cf added manifest to all jar files produced in maven reactor
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1584032 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 13:50:57 +00:00
Cédric Walter b8d70a3230 introduced dependencies management into main pom
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583975 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 11:40:33 +00:00
Cédric Walter b9dcd4a288 moved properties out of the build into pom.xmli
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583973 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 11:31:19 +00:00
Cédric Walter 3e55c1b0f0 moved poi scratchpad resources to module poi-scratchpad
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583972 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 11:29:39 +00:00
Cédric Walter 36c4ad6f84 moved poi scratchpad test to module poi-scratchpad
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583971 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 11:27:51 +00:00
Cédric Walter b04d63cad3 moved poi example code to module poi-examples
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583970 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 11:23:16 +00:00
Cédric Walter db81cd2456 moved excelant code to module poi-excelant
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583960 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 10:40:55 +00:00
Cédric Walter 8f4ebd091f moved excelant tests to module poi-excelant
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583959 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 10:37:55 +00:00
Cédric Walter 02109c4981 moved excelant resources to module poi-excelant
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583958 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 10:36:34 +00:00
Cédric Walter e8f75913e3 moved excelant code to module poi-excelant
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583956 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 10:35:40 +00:00
Cédric Walter 8f0f7e88c9 moved poi tests to module poi-main
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583915 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 07:33:56 +00:00
Cédric Walter adf677796e moved poi ressources to module poi-main
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583914 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 07:32:10 +00:00
Cédric Walter 8ca92da89f moved poi ressources to module poi-main
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583913 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 07:30:21 +00:00
Cédric Walter eb066c25a8 moved poi code to module poi-main
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583912 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 07:28:50 +00:00
Cédric Walter 7c6564c899 removed uneeded config in poi-ooxml module
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583911 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 07:26:59 +00:00
Cédric Walter 8683d22bba moved oxxml ressources to module
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583909 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 07:24:28 +00:00
Cédric Walter 8228f63d72 moved oxxml ressources to module
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583908 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 07:21:41 +00:00
Cédric Walter 89208cd986 moved oxxml tests to module
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583907 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 07:18:50 +00:00
Cédric Walter f3dc3b2414 moved oxxml resources to module
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583904 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 07:15:48 +00:00
Cédric Walter 66b3bd6306 moved oxxml code to module
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583902 13f79535-47bb-0310-9956-ffa450edef68
2014-04-02 07:13:59 +00:00
Cédric Walter f3f443d749 moved scratchpad code to module
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583654 13f79535-47bb-0310-9956-ffa450edef68
2014-04-01 12:56:41 +00:00
Cédric Walter 53eef9c574 added skeleton of maven modules
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583653 13f79535-47bb-0310-9956-ffa450edef68
2014-04-01 12:50:10 +00:00
Cédric Walter 6f19ff0cf9 created branch for maven spike
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/maven@1583646 13f79535-47bb-0310-9956-ffa450edef68
2014-04-01 12:09:54 +00:00
4240 changed files with 523695 additions and 92704 deletions

42
.ci/Jenkinsfile vendored
View File

@ -1,42 +0,0 @@
properties(
[
disableConcurrentBuilds()
]
)
node('linux && docker') {
try {
stage('Checkout') {
//branch name from Jenkins environment variables
echo "My branch is: ${env.BRANCH_NAME}"
// this doesn't grab tags pointing to this branch
//checkout scm
// this hack does... https://issues.jenkins.io/browse/JENKINS-45164
checkout([
$class: 'GitSCM',
branches: [[name: 'refs/heads/'+env.BRANCH_NAME]],
extensions: [[$class: 'CloneOption', noTags: false, shallow: false, depth: 0, reference: '']],
userRemoteConfigs: scm.userRemoteConfigs,
])
sh '''
set -euxo pipefail
git checkout "$BRANCH_NAME" --
git reset --hard "origin/$BRANCH_NAME"
'''
}
stage('Build + Deploy') {
sh 'curl --compressed -sL https://code.moparisthebest.com/moparisthebest/self-ci/raw/branch/master/build-ci.sh | bash'
}
currentBuild.result = 'SUCCESS'
} catch (Exception err) {
currentBuild.result = 'FAILURE'
} finally {
stage('Email') {
step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: 'admin.jenkins@moparisthebest.com', sendToIndividuals: true])
}
deleteDir()
}
}

View File

@ -1,36 +0,0 @@
#!/bin/bash
set -euxo pipefail
# Java 14+ java.lang.Record is ambiguous with hssf.Record :'(
[ $JAVA_VERSION -lt 7 -o $JAVA_VERSION -gt 13 ] && echo "build does not support JAVA_VERSION: $JAVA_VERSION" && exit 0
echo "starting build for JAVA_VERSION: $JAVA_VERSION"
# grab all deps with java 8
[ $JAVA_VERSION -eq 7 ] && run-java 8 mvn dependency:go-offline
# install deps
mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
if [ $JAVA_VERSION -lt 12 ]
then
# clean and test
mvn clean test -B
else
# clean and test
mvn clean test -B -Djava.version=7 # java12+ minimum target is 7, not 6
fi
# publish only from java 6 and master branch
if [ "$BRANCH_NAME" == "master" -a $JAVA_VERSION -eq 7 ]
then
echo 'deploying to maven'
# java 7 cannot do modern SSL, use java 8 to deploy
run-java 8 mvn deploy -Dmaven.test.skip=true -B
mkdir -p release
find -type f -name '*.jar' -print0 | xargs -0n1 -I {} mv '{}' 'release/'
fi
echo 'build success!'
exit 0

30
.classpath Normal file
View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/java"/>
<classpathentry kind="src" path="src/testcases"/>
<classpathentry kind="src" path="src/resources/main"/>
<classpathentry kind="src" path="src/ooxml/java"/>
<classpathentry kind="src" path="src/ooxml/testcases"/>
<classpathentry kind="src" path="src/resources/ooxml"/>
<classpathentry kind="src" path="src/scratchpad/src"/>
<classpathentry kind="src" path="src/scratchpad/testcases"/>
<classpathentry kind="src" path="src/resources/scratchpad"/>
<classpathentry kind="src" path="src/contrib/poi-ruby/java"/>
<classpathentry kind="src" path="src/examples/src"/>
<classpathentry kind="src" path="src/excelant/java"/>
<classpathentry kind="src" path="src/excelant/testcases"/>
<classpathentry kind="src" path="src/excelant/resources"/>
<classpathentry kind="lib" path="lib/ant-1.8.2.jar"/>
<classpathentry kind="lib" path="lib/commons-codec-1.9.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-1.1.3.jar"/>
<classpathentry kind="lib" path="lib/log4j-1.2.17.jar"/>
<classpathentry kind="lib" path="ooxml-lib/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="ooxml-lib/stax-api-1.0.1.jar"/>
<classpathentry kind="lib" path="ooxml-lib/xmlbeans-2.3.0.jar"/>
<classpathentry kind="lib" path="lib/hamcrest-core-1.3.jar"/>
<classpathentry kind="lib" path="lib/junit-4.11.jar"/>
<classpathentry kind="lib" path="ooxml-lib/ooxml-schemas-1.1.jar" sourcepath="ooxml-lib/ooxml-schemas-src-1.1.jar"/>
<classpathentry kind="lib" path="ooxml-lib/ooxml-encryption-1.1.jar" sourcepath="ooxml-lib/ooxml-encryption-src-1.1.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="build/eclipse"/>
</classpath>

22
.gitignore vendored
View File

@ -1,22 +0,0 @@
classes
workbook.xls
bak
*.iws
build.number
*.el
TEST-org.apache.poi*.xml
build
.settings
scripts
*.ipr
untitled1.jpx
*.iml
log*.*
dist
*.log
bin
.ant-targets-build.xml
out
.idea
.gradle
target

17
.project Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ApachePOI</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,301 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.codeComplete.argumentPrefixes=
org.eclipse.jdt.core.codeComplete.argumentSuffixes=
org.eclipse.jdt.core.codeComplete.fieldPrefixes=
org.eclipse.jdt.core.codeComplete.fieldSuffixes=
org.eclipse.jdt.core.codeComplete.localPrefixes=
org.eclipse.jdt.core.codeComplete.localSuffixes=
org.eclipse.jdt.core.codeComplete.staticFieldPrefixes=
org.eclipse.jdt.core.codeComplete.staticFieldSuffixes=
org.eclipse.jdt.core.codeComplete.staticFinalFieldPrefixes=
org.eclipse.jdt.core.codeComplete.staticFinalFieldSuffixes=
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_annotation=0
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_explicit_constructor_call=16
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_method_invocation=16
org.eclipse.jdt.core.formatter.alignment_for_arguments_in_qualified_allocation_expression=16
org.eclipse.jdt.core.formatter.alignment_for_assignment=0
org.eclipse.jdt.core.formatter.alignment_for_binary_expression=16
org.eclipse.jdt.core.formatter.alignment_for_compact_if=16
org.eclipse.jdt.core.formatter.alignment_for_conditional_expression=80
org.eclipse.jdt.core.formatter.alignment_for_enum_constants=0
org.eclipse.jdt.core.formatter.alignment_for_expressions_in_array_initializer=16
org.eclipse.jdt.core.formatter.alignment_for_method_declaration=0
org.eclipse.jdt.core.formatter.alignment_for_multiple_fields=16
org.eclipse.jdt.core.formatter.alignment_for_parameters_in_constructor_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_parameters_in_method_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_resources_in_try=80
org.eclipse.jdt.core.formatter.alignment_for_selector_in_method_invocation=16
org.eclipse.jdt.core.formatter.alignment_for_superclass_in_type_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_enum_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_superinterfaces_in_type_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_constructor_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_throws_clause_in_method_declaration=16
org.eclipse.jdt.core.formatter.alignment_for_union_type_in_multicatch=16
org.eclipse.jdt.core.formatter.blank_lines_after_imports=1
org.eclipse.jdt.core.formatter.blank_lines_after_package=1
org.eclipse.jdt.core.formatter.blank_lines_before_field=0
org.eclipse.jdt.core.formatter.blank_lines_before_first_class_body_declaration=0
org.eclipse.jdt.core.formatter.blank_lines_before_imports=1
org.eclipse.jdt.core.formatter.blank_lines_before_member_type=1
org.eclipse.jdt.core.formatter.blank_lines_before_method=1
org.eclipse.jdt.core.formatter.blank_lines_before_new_chunk=1
org.eclipse.jdt.core.formatter.blank_lines_before_package=0
org.eclipse.jdt.core.formatter.blank_lines_between_import_groups=1
org.eclipse.jdt.core.formatter.blank_lines_between_type_declarations=1
org.eclipse.jdt.core.formatter.brace_position_for_annotation_type_declaration=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_anonymous_type_declaration=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_array_initializer=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_block=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_block_in_case=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_constructor_declaration=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_enum_constant=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_enum_declaration=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_method_declaration=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_switch=end_of_line
org.eclipse.jdt.core.formatter.brace_position_for_type_declaration=end_of_line
org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_block_comment=false
org.eclipse.jdt.core.formatter.comment.clear_blank_lines_in_javadoc_comment=false
org.eclipse.jdt.core.formatter.comment.format_block_comments=true
org.eclipse.jdt.core.formatter.comment.format_header=false
org.eclipse.jdt.core.formatter.comment.format_html=true
org.eclipse.jdt.core.formatter.comment.format_javadoc_comments=true
org.eclipse.jdt.core.formatter.comment.format_line_comments=true
org.eclipse.jdt.core.formatter.comment.format_source_code=true
org.eclipse.jdt.core.formatter.comment.indent_parameter_description=true
org.eclipse.jdt.core.formatter.comment.indent_root_tags=true
org.eclipse.jdt.core.formatter.comment.insert_new_line_before_root_tags=insert
org.eclipse.jdt.core.formatter.comment.insert_new_line_for_parameter=insert
org.eclipse.jdt.core.formatter.comment.line_length=80
org.eclipse.jdt.core.formatter.comment.new_lines_at_block_boundaries=true
org.eclipse.jdt.core.formatter.comment.new_lines_at_javadoc_boundaries=true
org.eclipse.jdt.core.formatter.comment.preserve_white_space_between_code_and_line_comments=false
org.eclipse.jdt.core.formatter.compact_else_if=true
org.eclipse.jdt.core.formatter.continuation_indentation=2
org.eclipse.jdt.core.formatter.continuation_indentation_for_array_initializer=2
org.eclipse.jdt.core.formatter.disabling_tag=@formatter\:off
org.eclipse.jdt.core.formatter.enabling_tag=@formatter\:on
org.eclipse.jdt.core.formatter.format_guardian_clause_on_one_line=false
org.eclipse.jdt.core.formatter.format_line_comment_starting_on_first_column=true
org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_annotation_declaration_header=true
org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_constant_header=true
org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_enum_declaration_header=true
org.eclipse.jdt.core.formatter.indent_body_declarations_compare_to_type_header=true
org.eclipse.jdt.core.formatter.indent_breaks_compare_to_cases=true
org.eclipse.jdt.core.formatter.indent_empty_lines=false
org.eclipse.jdt.core.formatter.indent_statements_compare_to_block=true
org.eclipse.jdt.core.formatter.indent_statements_compare_to_body=true
org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_cases=true
org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch=false
org.eclipse.jdt.core.formatter.indentation.size=4
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_field=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_local_variable=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_method=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_package=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_parameter=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_after_annotation_on_type=insert
org.eclipse.jdt.core.formatter.insert_new_line_after_label=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_after_opening_brace_in_array_initializer=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_before_closing_brace_in_array_initializer=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_before_else_in_if_statement=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_before_finally_in_try_statement=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_before_while_in_do_statement=do not insert
org.eclipse.jdt.core.formatter.insert_new_line_in_empty_annotation_declaration=insert
org.eclipse.jdt.core.formatter.insert_new_line_in_empty_anonymous_type_declaration=insert
org.eclipse.jdt.core.formatter.insert_new_line_in_empty_block=insert
org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_constant=insert
org.eclipse.jdt.core.formatter.insert_new_line_in_empty_enum_declaration=insert
org.eclipse.jdt.core.formatter.insert_new_line_in_empty_method_body=insert
org.eclipse.jdt.core.formatter.insert_new_line_in_empty_type_declaration=insert
org.eclipse.jdt.core.formatter.insert_space_after_and_in_type_parameter=insert
org.eclipse.jdt.core.formatter.insert_space_after_assignment_operator=insert
org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_at_in_annotation_type_declaration=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_binary_operator=insert
org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_arguments=insert
org.eclipse.jdt.core.formatter.insert_space_after_closing_angle_bracket_in_type_parameters=insert
org.eclipse.jdt.core.formatter.insert_space_after_closing_brace_in_block=insert
org.eclipse.jdt.core.formatter.insert_space_after_closing_paren_in_cast=insert
org.eclipse.jdt.core.formatter.insert_space_after_colon_in_assert=insert
org.eclipse.jdt.core.formatter.insert_space_after_colon_in_case=insert
org.eclipse.jdt.core.formatter.insert_space_after_colon_in_conditional=insert
org.eclipse.jdt.core.formatter.insert_space_after_colon_in_for=insert
org.eclipse.jdt.core.formatter.insert_space_after_colon_in_labeled_statement=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_allocation_expression=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_annotation=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_array_initializer=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_constructor_declaration_parameters=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_constructor_declaration_throws=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_constant_arguments=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_enum_declarations=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_explicitconstructorcall_arguments=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_for_increments=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_for_inits=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_declaration_parameters=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_declaration_throws=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_method_invocation_arguments=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_multiple_field_declarations=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_multiple_local_declarations=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_parameterized_type_reference=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_superinterfaces=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_arguments=insert
org.eclipse.jdt.core.formatter.insert_space_after_comma_in_type_parameters=insert
org.eclipse.jdt.core.formatter.insert_space_after_ellipsis=insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_parameterized_type_reference=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_arguments=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_angle_bracket_in_type_parameters=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_brace_in_array_initializer=insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_bracket_in_array_allocation_expression=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_bracket_in_array_reference=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_annotation=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_cast=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_catch=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_constructor_declaration=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_enum_constant=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_for=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_if=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_declaration=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_method_invocation=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_parenthesized_expression=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_switch=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_synchronized=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_try=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_opening_paren_in_while=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_postfix_operator=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_prefix_operator=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_question_in_conditional=insert
org.eclipse.jdt.core.formatter.insert_space_after_question_in_wildcard=do not insert
org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_for=insert
org.eclipse.jdt.core.formatter.insert_space_after_semicolon_in_try_resources=insert
org.eclipse.jdt.core.formatter.insert_space_after_unary_operator=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_and_in_type_parameter=insert
org.eclipse.jdt.core.formatter.insert_space_before_assignment_operator=insert
org.eclipse.jdt.core.formatter.insert_space_before_at_in_annotation_type_declaration=insert
org.eclipse.jdt.core.formatter.insert_space_before_binary_operator=insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_parameterized_type_reference=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_arguments=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_angle_bracket_in_type_parameters=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_brace_in_array_initializer=insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_bracket_in_array_allocation_expression=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_bracket_in_array_reference=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_annotation=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_cast=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_catch=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_constructor_declaration=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_enum_constant=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_for=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_if=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_method_declaration=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_method_invocation=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_parenthesized_expression=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_switch=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_synchronized=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_try=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_closing_paren_in_while=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_colon_in_assert=insert
org.eclipse.jdt.core.formatter.insert_space_before_colon_in_case=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_colon_in_conditional=insert
org.eclipse.jdt.core.formatter.insert_space_before_colon_in_default=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_colon_in_for=insert
org.eclipse.jdt.core.formatter.insert_space_before_colon_in_labeled_statement=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_allocation_expression=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_annotation=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_array_initializer=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_constructor_declaration_parameters=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_constructor_declaration_throws=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_enum_constant_arguments=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_enum_declarations=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_explicitconstructorcall_arguments=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_for_increments=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_for_inits=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_declaration_parameters=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_declaration_throws=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_method_invocation_arguments=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_field_declarations=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_multiple_local_declarations=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_parameterized_type_reference=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_superinterfaces=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_arguments=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_comma_in_type_parameters=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_ellipsis=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_parameterized_type_reference=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_arguments=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_parameters=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_annotation_type_declaration=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_anonymous_type_declaration=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_array_initializer=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_block=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_constructor_declaration=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_constant=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_enum_declaration=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_method_declaration=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_switch=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_brace_in_type_declaration=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_allocation_expression=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_reference=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_bracket_in_array_type_reference=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_annotation=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_annotation_type_member_declaration=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_catch=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_constructor_declaration=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_enum_constant=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_for=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_if=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_declaration=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_method_invocation=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_parenthesized_expression=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_switch=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_synchronized=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_try=insert
org.eclipse.jdt.core.formatter.insert_space_before_opening_paren_in_while=insert
org.eclipse.jdt.core.formatter.insert_space_before_parenthesized_expression_in_return=insert
org.eclipse.jdt.core.formatter.insert_space_before_parenthesized_expression_in_throw=insert
org.eclipse.jdt.core.formatter.insert_space_before_postfix_operator=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_prefix_operator=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_question_in_conditional=insert
org.eclipse.jdt.core.formatter.insert_space_before_question_in_wildcard=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_semicolon=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_for=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_semicolon_in_try_resources=do not insert
org.eclipse.jdt.core.formatter.insert_space_before_unary_operator=do not insert
org.eclipse.jdt.core.formatter.insert_space_between_brackets_in_array_type_reference=do not insert
org.eclipse.jdt.core.formatter.insert_space_between_empty_braces_in_array_initializer=do not insert
org.eclipse.jdt.core.formatter.insert_space_between_empty_brackets_in_array_allocation_expression=do not insert
org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_annotation_type_member_declaration=do not insert
org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_constructor_declaration=do not insert
org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_enum_constant=do not insert
org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_declaration=do not insert
org.eclipse.jdt.core.formatter.insert_space_between_empty_parens_in_method_invocation=do not insert
org.eclipse.jdt.core.formatter.join_lines_in_comments=true
org.eclipse.jdt.core.formatter.join_wrapped_lines=true
org.eclipse.jdt.core.formatter.keep_else_statement_on_same_line=false
org.eclipse.jdt.core.formatter.keep_empty_array_initializer_on_one_line=false
org.eclipse.jdt.core.formatter.keep_imple_if_on_one_line=false
org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line=false
org.eclipse.jdt.core.formatter.lineSplit=80
org.eclipse.jdt.core.formatter.never_indent_block_comments_on_first_column=false
org.eclipse.jdt.core.formatter.never_indent_line_comments_on_first_column=false
org.eclipse.jdt.core.formatter.number_of_blank_lines_at_beginning_of_method_body=0
org.eclipse.jdt.core.formatter.number_of_empty_lines_to_preserve=1
org.eclipse.jdt.core.formatter.put_empty_statement_on_new_line=true
org.eclipse.jdt.core.formatter.tabulation.char=space
org.eclipse.jdt.core.formatter.tabulation.size=4
org.eclipse.jdt.core.formatter.use_on_off_tags=false
org.eclipse.jdt.core.formatter.use_tabs_only_for_leading_indentations=false
org.eclipse.jdt.core.formatter.wrap_before_binary_operator=true
org.eclipse.jdt.core.formatter.wrap_before_or_operator_multicatch=true
org.eclipse.jdt.core.formatter.wrap_outer_expressions_when_nested=true

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,4 @@
eclipse.preferences.version=1
org.moreunit.core.anyLanguage.srcFolderPathTemplate=${srcProject}
org.moreunit.core.anyLanguage.testFileNameTemplate=${srcFile}Test
org.moreunit.core.anyLanguage.testFolderPathTemplate=${srcProject}

1607
KEYS Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,24 +0,0 @@
POI Fast Calc
======================
[![Build Status](https://ci.moparisthe.best/job/moparisthebest/job/poi/job/master/badge/icon%3Fstyle=plastic)](https://ci.moparisthe.best/job/moparisthebest/job/poi/job/master/)
A Java library to calculate Excel formulas quickly.
This is a fork of [Apache POI](https://poi.apache.org/) version [3.16](https://github.com/apache/poi/tree/REL_3_16_FINAL)
that serves simply to calculate formulas quickly, it supports XLSX (Excel 2007) row/column limits in the HSSF engine for
much faster evaluation than XML-backed XSSF is capable of, with the drawback that it can't read or write XLS/XLSX files
from or to disk. Read the [email thread](https://lists.apache.org/thread.html/0bc90a3ed386edddfcb9b93ce6c262ad145a6b0433d0fcfe70ef10a2@%3Cdev.poi.apache.org%3E)
with my original proposed patch to upstream poi for background.
To use, add this to your maven pom.xml:
```xml
<dependency>
<groupId>com.moparisthebest.poi</groupId>
<artifactId>poi-fast-calc</artifactId>
<version>3.16-SNAPSHOT</version>
</dependency>
```
The `org.apache.poi` package has been renamed `com.moparisthebest.poi` and all dependencies removed,
so this can cleanly live aside modern/newer upstream poi forever, and shouldn't ever need to change.

71
doap_POI.rdf Normal file
View File

@ -0,0 +1,71 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl"?>
<rdf:RDF xml:lang="en"
xmlns="http://usefulinc.com/ns/doap#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:asfext="http://projects.apache.org/ns/asfext#"
xmlns:foaf="http://xmlns.com/foaf/0.1/">
<Project rdf:about="http://poi.apache.org">
<created>2006-01-26</created>
<license rdf:resource="http://usefulinc.com/doap/licenses/asl20" />
<name>Apache POI</name>
<homepage rdf:resource="http://poi.apache.org" />
<asfext:pmc rdf:resource="http://poi.apache.org" />
<shortdesc>Java API To Access Microsoft Document File Formats</shortdesc>
<description>APIs for manipulating various file formats based upon Open Office XML (ECMA-376) and Microsoft's OLE 2 Compound Document formats using pure Java. Apache POI is your Java Excel, Word and PowerPoint solution. We have a complete API for porting other OOXML and OLE 2 Compound Document formats and welcome others to participate.</description>
<bug-database rdf:resource="http://issues.apache.org/bugzilla/buglist.cgi?product=POI" />
<mailing-list rdf:resource="http://poi.apache.org/mailinglists.html" />
<download-page rdf:resource="http://www.apache.org/dyn/closer.cgi/poi/" />
<programming-language>Java</programming-language>
<category rdf:resource="http://projects.apache.org/category/content" />
<category rdf:resource="http://projects.apache.org/category/library" />
<release>
<Version>
<name>Apache POI 3.9</name>
<created>2012-12-03</created>
<revision>3.9</revision>
</Version>
<Version>
<name>Apache POI 3.8</name>
<created>2012-03-26</created>
<revision>3.8</revision>
</Version>
<Version>
<name>Apache POI 3.7</name>
<created>2010-10-29</created>
<revision>3.7</revision>
</Version>
<Version>
<name>Apache POI 3.6</name>
<created>2009-12-14</created>
<revision>3.6</revision>
</Version>
</release>
<release>
<Version>
<name>Apache POI 3.5</name>
<created>2009-09-28</created>
<revision>3.5</revision>
</Version>
</release>
<release>
<Version>
<name>Apache POI 3.2</name>
<created>2008-10-19</created>
<revision>3.2</revision>
</Version>
</release>
<repository>
<SVNRepository>
<location rdf:resource="http://svn.apache.org/repos/asf/poi/"/>
<browse rdf:resource="http://svn.apache.org/viewvc/poi/"/>
</SVNRepository>
</repository>
<maintainer>
<foaf:Person>
<foaf:name>Yegor Kozlov</foaf:name>
<foaf:mbox rdf:resource="mailto:yegor@apache.org"/>
</foaf:Person>
</maintainer>
</Project>
</rdf:RDF>

26
etc/assembly/bundle.xml Normal file
View File

@ -0,0 +1,26 @@
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<!--<id>dist</id>-->
<!--<formats>-->
<!--<format>zip</format>-->
<!--</formats>-->
<!--<includeBaseDirectory>false</includeBaseDirectory>-->
<!--<dependencySets>-->
<!--<dependencySet>-->
<!--<includes>-->
<!--<include>org.apache.poi:poi-examples:jar</include>-->
<!--<include>org.apache.poi:poi-excelant:jar</include>-->
<!--<include>org.apache.poi:poi-main:jar</include>-->
<!--<include>org.apache.poi:poi-ooxml:jar</include>-->
<!--<include>org.apache.poi:poi-ooxml-schemas:jar</include>-->
<!--<include>org.apache.poi:poi-scratchpad:jar</include>-->
<!--</includes>-->
<!--<unpack>false</unpack>-->
<!--<outputDirectory>dist</outputDirectory>-->
<!--</dependencySet>-->
<!--</dependencySets>-->
</assembly>

View File

@ -209,7 +209,7 @@ license terms. Your use of these subcomponents is subject to the terms
and conditions of the following licenses:
Office Open XML schemas (ooxml-schemas-1.*.jar)
Office Open XML schemas (ooxml-schemas-1.1.jar)
The Office Open XML schema definitions used by Apache POI are
a part of the Office Open XML ECMA Specification (ECMA-376, [1]).
@ -225,64 +225,92 @@ Office Open XML schemas (ooxml-schemas-1.*.jar)
[1] http://www.ecma-international.org/publications/standards/Ecma-376.htm
[2] http://www.ecma-international.org/memento/Ecmabylaws.htm
[3] http://www.microsoft.com/openspecifications/en/us/programs/osp/default.aspx
[4] http://www.ecma-international.org/publications/files/ECMA-ST/Ecma%20PATENT/
Patent%20statements%20ok/ECMA-376%20Edition%202%20Microsoft%20Patent%20Declaration.pdf
[5] http://www.ecma-international.org/publications/files/ECMA-ST/Ecma%20PATENT/
Patent%20statements%20ok/ECMA-376%20Adobe%20Patent%20Declaration.pdf
[4] http://www.ecma-international.org/publications/files/ECMA-ST/Ecma%20PATENT/Patent%20statements%20ok/ECMA-376%20Edition%202%20Microsoft%20Patent%20Declaration.pdf
[5] http://www.ecma-international.org/publications/files/ECMA-ST/Ecma%20PATENT/Patent%20statements%20ok/ECMA-376%20Adobe%20Patent%20Declaration.pdf
Bouncy Castle library (bcprov-*.jar, bcpg-*.jar, bcpkix-*.jar)
DOM4J library (dom4j-1.6.1.jar)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
Redistribution and use of this software and associated documentation
("Software"), with or without modification, are permitted provided
that the following conditions are met:
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
1. Redistributions of source code must retain copyright
statements and notices. Redistributions must also contain a
copy of this document.
JUnit test library (junit-4.*.jar) & JaCoCo (*jacoco*)
2. Redistributions in binary form must reproduce the
above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
Eclipse Public License - v 1.0
3. The name "DOM4J" must not be used to endorse or promote
products derived from this Software without prior written
permission of MetaStuff, Ltd. For written permission,
please contact dom4j-info@metastuff.com.
THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC
LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM
CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
4. Products derived from this Software may not be called "DOM4J"
nor may "DOM4J" appear in their names without prior written
permission of MetaStuff, Ltd. DOM4J is a registered
trademark of MetaStuff, Ltd.
5. Due credit should be given to the DOM4J Project -
http://www.dom4j.org
THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
JUnit test library (junit-4.11.jar)
Common Public License - v 1.0
THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS COMMON
PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION
OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
1. DEFINITIONS
"Contribution" means:
a) in the case of the initial Contributor, the initial code and documentation
distributed under this Agreement, and
a) in the case of the initial Contributor, the initial code and
documentation distributed under this Agreement, and
b) in the case of each subsequent Contributor:
i) changes to the Program, and
ii) additions to the Program;
where such changes and/or additions to the Program originate from and are
distributed by that particular Contributor. A Contribution 'originates' from
a Contributor if it was added to the Program by such Contributor itself or
anyone acting on such Contributor's behalf. Contributions do not include
additions to the Program which: (i) are separate modules of software
distributed in conjunction with the Program under their own license agreement,
and (ii) are not derivative works of the Program.
i) changes to the Program, and
ii) additions to the Program;
where such changes and/or additions to the Program originate from
and are distributed by that particular Contributor. A Contribution
'originates' from a Contributor if it was added to the Program by
such Contributor itself or anyone acting on such Contributor's behalf.
Contributions do not include additions to the Program which: (i) are
separate modules of software distributed in conjunction with the
Program under their own license agreement, and (ii) are not derivative
works of the Program.
"Contributor" means any person or entity that distributes the Program.
"Licensed Patents" mean patent claims licensable by a Contributor which are
necessarily infringed by the use or sale of its Contribution alone or when
combined with the Program.
"Licensed Patents " mean patent claims licensable by a Contributor which
are necessarily infringed by the use or sale of its Contribution alone
or when combined with the Program.
"Program" means the Contributions distributed in accordance with this Agreement.
"Program" means the Contributions distributed in accordance with this
Agreement.
"Recipient" means anyone who receives the Program under this Agreement,
including all Contributors.
@ -290,248 +318,190 @@ JUnit test library (junit-4.*.jar) & JaCoCo (*jacoco*)
2. GRANT OF RIGHTS
a) Subject to the terms of this Agreement, each Contributor hereby grants
Recipient a non-exclusive, worldwide, royalty-free copyright license to
reproduce, prepare derivative works of, publicly display, publicly
perform, distribute and sublicense the Contribution of such Contributor,
if any, and such derivative works, in source code and object code form.
Recipient a non-exclusive, worldwide, royalty-free copyright license
to reproduce, prepare derivative works of, publicly display, publicly
perform, distribute and sublicense the Contribution of such
Contributor, if any, and such derivative works, in source code and
object code form.
b) Subject to the terms of this Agreement, each Contributor hereby grants
Recipient a non-exclusive, worldwide, royalty-free patent license under
Licensed Patents to make, use, sell, offer to sell, import and otherwise
transfer the Contribution of such Contributor, if any, in source code
and object code form. This patent license shall apply to the combination
of the Contribution and the Program if, at the time the Contribution is
added by the Contributor, such addition of the Contribution causes such
combination to be covered by the Licensed Patents. The patent license
shall not apply to any other combinations which include the Contribution.
No hardware per se is licensed hereunder.
c) Recipient understands that although each Contributor grants the licenses
to its Contributions set forth herein, no assurances are provided by any
Contributor that the Program does not infringe the patent or other
intellectual property rights of any other entity. Each Contributor
disclaims any liability to Recipient for claims brought by any other
entity based on infringement of intellectual property rights or
otherwise. As a condition to exercising the rights and licenses granted
hereunder, each Recipient hereby assumes sole responsibility to secure
any other intellectual property rights needed, if any. For example, if
a third party patent license is required to allow Recipient to distribute
the Program, it is Recipient's responsibility to acquire that license
before distributing the Program.
Licensed Patents to make, use, sell, offer to sell, import and
otherwise transfer the Contribution of such Contributor, if any, in
source code and object code form. This patent license shall apply to
the combination of the Contribution and the Program if, at the time
the Contribution is added by the Contributor, such addition of the
Contribution causes such combination to be covered by the Licensed
Patents. The patent license shall not apply to any other combinations
which include the Contribution. No hardware per se is licensed
hereunder.
c) Recipient understands that although each Contributor grants the
licenses to its Contributions set forth herein, no assurances are
provided by any Contributor that the Program does not infringe the
patent or other intellectual property rights of any other entity.
Each Contributor disclaims any liability to Recipient for claims
brought by any other entity based on infringement of intellectual
property rights or otherwise. As a condition to exercising the rights
and licenses granted hereunder, each Recipient hereby assumes sole
responsibility to secure any other intellectual property rights
needed, if any. For example, if a third party patent license is
required to allow Recipient to distribute the Program, it is
Recipient's responsibility to acquire that license before
distributing the Program.
d) Each Contributor represents that to its knowledge it has sufficient
copyright rights in its Contribution, if any, to grant the copyright
license set forth in this Agreement.
3. REQUIREMENTS
A Contributor may choose to distribute the Program in object code form under
its own license agreement, provided that:
A Contributor may choose to distribute the Program in object code form
under its own license agreement, provided that:
a) it complies with the terms and conditions of this Agreement; and
b) its license agreement:
i) effectively disclaims on behalf of all Contributors all warranties and
conditions, express and implied, including warranties or conditions of
title and non-infringement, and implied warranties or conditions of
merchantability and fitness for a particular purpose;
ii) effectively excludes on behalf of all Contributors all liability for
damages, including direct, indirect, special, incidental and
consequential damages, such as lost profits;
iii) states that any provisions which differ from this Agreement are
offered by that Contributor alone and not by any other party; and
iv) states that source code for the Program is available from such
Contributor, and informs licensees how to obtain it in a reasonable
manner on or through a medium customarily used for software exchange.
i) effectively disclaims on behalf of all Contributors all warranties
and conditions, express and implied, including warranties or
conditions of title and non-infringement, and implied warranties
or conditions of merchantability and fitness for a particular
purpose;
ii) effectively excludes on behalf of all Contributors all liability
for damages, including direct, indirect, special, incidental and
consequential damages, such as lost profits;
iii) states that any provisions which differ from this Agreement are
offered by that Contributor alone and not by any other party; and
iv) states that source code for the Program is available from such
Contributor, and informs licensees how to obtain it in a
reasonable manner on or through a medium customarily used for
software exchange.
When the Program is made available in source code form:
a) it must be made available under this Agreement; and
b) a copy of this Agreement must be included with each copy of the Program.
Contributors may not remove or alter any copyright notices contained
within the Program.
Each Contributor must identify itself as the originator of its Contribution,
if any, in a manner that reasonably allows subsequent Recipients to identify
the originator of the Contribution.
b) a copy of this Agreement must be included with each copy of
the Program.
Contributors may not remove or alter any copyright notices contained
within the Program.
Each Contributor must identify itself as the originator of its
Contribution, if any, in a manner that reasonably allows subsequent
Recipients to identify the originator of the Contribution.
4. COMMERCIAL DISTRIBUTION
Commercial distributors of software may accept certain responsibilities with
respect to end users, business partners and the like. While this license is
intended to facilitate the commercial use of the Program, the Contributor
who includes the Program in a commercial product offering should do so in a
manner which does not create potential liability for other Contributors.
Therefore, if a Contributor includes the Program in a commercial product
offering, such Contributor ("Commercial Contributor") hereby agrees to
defend and indemnify every other Contributor ("Indemnified Contributor")
against any losses, damages and costs (collectively "Losses") arising from
claims, lawsuits and other legal actions brought by a third party against
the Indemnified Contributor to the extent caused by the acts or omissions
of such Commercial Contributor in connection with its distribution of the
Program in a commercial product offering. The obligations in this section
do not apply to any claims or Losses relating to any actual or alleged
intellectual property infringement. In order to qualify, an Indemnified
Contributor must: a) promptly notify the Commercial Contributor in writing
of such claim, and b) allow the Commercial Contributor to control, and
cooperate with the Commercial Contributor in, the defense and any related
settlement negotiations. The Indemnified Contributor may participate in any
such claim at its own expense.
Commercial distributors of software may accept certain responsibilities
with respect to end users, business partners and the like. While this
license is intended to facilitate the commercial use of the Program,
the Contributor who includes the Program in a commercial product offering
should do so in a manner which does not create potential liability for
other Contributors. Therefore, if a Contributor includes the Program
in a commercial product offering, such Contributor ("Commercial
Contributor") hereby agrees to defend and indemnify every other
Contributor ("Indemnified Contributor") against any losses, damages
and costs (collectively "Losses") arising from claims, lawsuits and
other legal actions brought by a third party against the Indemnified
Contributor to the extent caused by the acts or omissions of such
Commercial Contributor in connection with its distribution of the
Program in a commercial product offering. The obligations in this
section do not apply to any claims or Losses relating to any actual
or alleged intellectual property infringement. In order to qualify,
an Indemnified Contributor must: a) promptly notify the Commercial
Contributor in writing of such claim, and b) allow the Commercial
Contributor to control, and cooperate with the Commercial Contributor
in, the defense and any related settlement negotiations. The Indemnified
Contributor may participate in any such claim at its own expense.
For example, a Contributor might include the Program in a commercial product
offering, Product X. That Contributor is then a Commercial Contributor. If
that Commercial Contributor then makes performance claims, or offers
warranties related to Product X, those performance claims and warranties are
such Commercial Contributor's responsibility alone. Under this section, the
Commercial Contributor would have to defend claims against the other
Contributors related to those performance claims and warranties, and if a
court requires any other Contributor to pay any damages as a result, the
Commercial Contributor must pay those damages.
For example, a Contributor might include the Program in a commercial
product offering, Product X. That Contributor is then a Commercial
Contributor. If that Commercial Contributor then makes performance
claims, or offers warranties related to Product X, those performance
claims and warranties are such Commercial Contributor's responsibility
alone. Under this section, the Commercial Contributor would have to
defend claims against the other Contributors related to those
performance claims and warranties, and if a court requires any other
Contributor to pay any damages as a result, the Commercial Contributor
must pay those damages.
5. NO WARRANTY
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON
AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED
ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER
EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR
CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A
PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the
appropriateness of using and distributing the Program and assumes all risks
associated with its exercise of rights under this Agreement , including but
not limited to the risks and costs of program errors, compliance with
applicable laws, damage to or loss of data, programs or equipment, and
unavailability or interruption of operations.
CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR
A PARTICULAR PURPOSE. Each Recipient is solely responsible for
determining the appropriateness of using and distributing the Program
and assumes all risks associated with its exercise of rights under this
Agreement, including but not limited to the risks and costs of program
errors, compliance with applicable laws, damage to or loss of data,
programs or equipment, and unavailability or interruption of operations.
6. DISCLAIMER OF LIABILITY
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY
CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION
LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE
EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGES.
EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR
ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING
WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR
DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
7. GENERAL
If any provision of this Agreement is invalid or unenforceable under
applicable law, it shall not affect the validity or enforceability of the
remainder of the terms of this Agreement, and without further action by the
parties hereto, such provision shall be reformed to the minimum extent
necessary to make such provision valid and enforceable.
applicable law, it shall not affect the validity or enforceability of
the remainder of the terms of this Agreement, and without further
action by the parties hereto, such provision shall be reformed to the
minimum extent necessary to make such provision valid and enforceable.
If Recipient institutes patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Program itself
(excluding combinations of the Program with other software or hardware)
infringes such Recipient's patent(s), then such Recipient's rights granted
under Section 2(b) shall terminate as of the date such litigation is filed.
If Recipient institutes patent litigation against a Contributor with
respect to a patent applicable to software (including a cross-claim or
counterclaim in a lawsuit), then any patent licenses granted by that
Contributor to such Recipient under this Agreement shall terminate as of
the date such litigation is filed. In addition, if Recipient institutes
patent litigation against any entity (including a cross-claim or
counterclaim in a lawsuit) alleging that the Program itself (excluding
combinations of the Program with other software or hardware) infringes
such Recipient's patent(s), then such Recipient's rights granted under
Section 2(b) shall terminate as of the date such litigation is filed.
All Recipient's rights under this Agreement shall terminate if it fails to
comply with any of the material terms or conditions of this Agreement and
does not cure such failure in a reasonable period of time after becoming
aware of such noncompliance. If all Recipient's rights under this Agreement
terminate, Recipient agrees to cease use and distribution of the Program as
soon as reasonably practicable. However, Recipient's obligations under this
Agreement and any licenses granted by Recipient relating to the Program
shall continue and survive.
All Recipient's rights under this Agreement shall terminate if it fails
to comply with any of the material terms or conditions of this Agreement
and does not cure such failure in a reasonable period of time after
becoming aware of such noncompliance. If all Recipient's rights under
this Agreement terminate, Recipient agrees to cease use and distribution
of the Program as soon as reasonably practicable. However, Recipient's
obligations under this Agreement and any licenses granted by Recipient
relating to the Program shall continue and survive.
Everyone is permitted to copy and distribute copies of this Agreement, but
in order to avoid inconsistency the Agreement is copyrighted and may only
be modified in the following manner. The Agreement Steward reserves the
right to publish new versions (including revisions) of this Agreement from
time to time. No one other than the Agreement Steward has the right to
modify this Agreement. The Eclipse Foundation is the initial Agreement
Steward. The Eclipse Foundation may assign the responsibility to serve as
the Agreement Steward to a suitable separate entity. Each new version of
the Agreement will be given a distinguishing version number. The Program
(including Contributions) may always be distributed subject to the version
of the Agreement under which it was received. In addition, after a new
version of the Agreement is published, Contributor may elect to distribute
the Program (including its Contributions) under the new version. Except as
expressly stated in Sections 2(a) and 2(b) above, Recipient receives no
rights or licenses to the intellectual property of any Contributor under
this Agreement, whether expressly, by implication, estoppel or otherwise.
All rights in the Program not expressly granted under this Agreement are
reserved.
Everyone is permitted to copy and distribute copies of this Agreement,
but in order to avoid inconsistency the Agreement is copyrighted and may
only be modified in the following manner. The Agreement Steward reserves
the right to publish new versions (including revisions) of this Agreement
from time to time. No one other than the Agreement Steward has the right
to modify this Agreement. IBM is the initial Agreement Steward. IBM may
assign the responsibility to serve as the Agreement Steward to a suitable
separate entity. Each new version of the Agreement will be given a
distinguishing version number. The Program (including Contributions) may
always be distributed subject to the version of the Agreement under which
it was received. In addition, after a new version of the Agreement is
published, Contributor may elect to distribute the Program (including
its Contributions) under the new version. Except as expressly stated in
Sections 2(a) and 2(b) above, Recipient receives no rights or licenses
to the intellectual property of any Contributor under this Agreement,
whether expressly, by implication, estoppel or otherwise. All rights in
the Program not expressly granted under this Agreement are reserved.
This Agreement is governed by the laws of the State of New York and the
intellectual property laws of the United States of America. No party to this
Agreement will bring a legal action under this Agreement more than one year
after the cause of action arose. Each party waives its rights to a jury
trial in any resulting litigation.
Hamcrest library (hamcrest-*.jar) & CuvesAPI / Curve API
BSD License
Copyright (c) 2000-2006, www.hamcrest.org
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer. Redistributions in binary
form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials
provided with the distribution.
Neither the name of Hamcrest nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
SLF4J library (slf4j-api-*.jar)
Copyright (c) 2004-2013 QOS.ch
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
inbot-utils (https://github.com/Inbot/inbot-utils)
The MIT License (MIT)
Copyright (c) 2015 Inbot
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
intellectual property laws of the United States of America. No party to
this Agreement will bring a legal action under this Agreement more than
one year after the cause of action arose. Each party waives its rights
to a jury trial in any resulting litigation.

View File

@ -1,14 +1,14 @@
Apache POI
Copyright 2003-2017 The Apache Software Foundation
Copyright 2003-2014 The Apache Software Foundation
This product includes software developed by
The Apache Software Foundation (https://www.apache.org/).
The Apache Software Foundation (http://www.apache.org/).
This product contains the DOM4J library (http://www.dom4j.org).
Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
This product contains parts that were originally based on software from BEA.
Copyright (c) 2000-2003, BEA Systems, <http://www.bea.com/> (dead link),
which was acquired by Oracle Corporation in 2008.
<http://www.oracle.com/us/corporate/Acquisitions/bea/index.html>
<https://en.wikipedia.org/wiki/BEA_Systems>
Copyright (c) 2000-2003, BEA Systems, <http://www.bea.com/>.
This product contains W3C XML Schema documents. Copyright 2001-2003 (c)
World Wide Web Consortium (Massachusetts Institute of Technology, European
@ -19,9 +19,3 @@ This product contains the Piccolo XML Parser for Java
This product contains the chunks_parse_cmds.tbl file from the vsdump program.
Copyright (C) 2006-2007 Valek Filippov (frob@df.ru)
This product contains parts of the eID Applet project
<http://eid-applet.googlecode.com> and <https://github.com/e-Contract/eid-applet>.
Copyright (c) 2009-2014
FedICT (federal ICT department of Belgium), e-Contract.be BVBA (https://www.e-contract.be),
Bart Hanssens from FedICT

50
maven/multisign.sh Executable file
View File

@ -0,0 +1,50 @@
#! /bin/sh
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# create md5 checksums and gpg signatures
stty -echo
echo "enter your GPG passphrase"
read passphrase
stty echo
# Do we have md5sum and sha1sum?
# (We can use openssl if not, but the files it produces aren't as nice)
which md5sum > /dev/null
LACKING_MD5SUM=$?
which sha1sum > /dev/null
LACKING_SHA1SUM=$?
for i in *; do
echo ""
echo Signing $i
echo $passphrase | gpg --passphrase-fd 0 --output $i.asc --detach-sig --armor $i
gpg --verify $i.asc $i
echo Hashing $i
if [ "$LACKING_MD5SUM" = "1" ]; then
openssl md5 < $i > $i.md5
else
md5sum $i > $i.md5
fi
if [ "$LACKING_SHA1SUM" = "1" ]; then
openssl sha1 < $i > $i.sha1
else
sha1sum $i > $i.sha1
fi
done

63
maven/mvn-deploy.sh Executable file
View File

@ -0,0 +1,63 @@
#! /bin/sh
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Shell script to deploy POI artifacts in a maven repository.
#
# Note, You should configure your settings.xml and add a server with id=apache-releases:
#
# <server>
# <id>apache-releases</id>
# <username>apacheId</username>
# <privateKey>/path/to/private/key</privateKey>
# </server>
#
# <profiles>
# <profile>
# <id>apache-releases</id>
# <properties>
# <gpg.passphrase><!-- Your GPG passphrase --></gpg.passphrase>
# </properties>
# </profile>
# </profiles>
#
# Usage:
# 1. ant dist
# 2. cd build/dist
# 3. ./mvn-deploy.sh
# @author Yegor Kozlov
M2_REPOSITORY=scp://people.apache.org/www/people.apache.org/repo/m2-ibiblio-rsync-repository
M2_SCP=people.apache.org:/www/people.apache.org/repo/m2-ibiblio-rsync-repository
VERSION=@VERSION@
DSTAMP=@DSTAMP@
for artifactId in poi poi-scratchpad poi-ooxml poi-examples poi-ooxml-schemas poi-excelant
do
mvn gpg:sign-and-deploy-file -DrepositoryId=apache-releases -P apache-releases \
-Durl=$M2_REPOSITORY \
-Dfile=$artifactId-$VERSION-$DSTAMP.jar -DpomFile=$artifactId-$VERSION.pom
#The maven sign-and-deploy-file command does NOT sign POM files, so we have to upload the POM's .asc manually
scp $artifactId-$VERSION.pom.asc $M2_SCP/org/apache/poi/$artifactId/$VERSION/
if [ -r $artifactId-$VERSION-sources-$DSTAMP.jar ]; then
mvn deploy:deploy-file -DrepositoryId=apache-releases -P apache-releases \
-Durl=$M2_REPOSITORY -DgeneratePom=false -Dpackaging=java-source \
-Dfile=$artifactId-$VERSION-sources-$DSTAMP.jar -DpomFile=$artifactId-$VERSION.pom
scp $artifactId-$VERSION-sources-$DSTAMP.jar.asc $M2_SCP/org/apache/poi/$artifactId/$VERSION/$artifactId-$VERSION-sources.jar.asc
fi
done

924
old.xml Normal file
View File

@ -0,0 +1,924 @@
<?xml version="1.0"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
POI Build System. Written by:
Glen Stampoultzis glens at apache.org
Modified by:
Rainer Klute klute@rainer-klute.de
Bruno Girin brunogirin@gmail.com
Yegor Kozlov yegor at apache.org
This build was tested with ant 1.6.2 although it will probably work with
other versions. The following jar files should be available on the
classpath when running ant:
LIBRARY LOCATION
======= ========
junit(3.8+) http://www.junit.org
To build the documentation you will need to install forrest and set
the FORREST_HOME environment variable. Forrest 0.5.1 required.
Since POI 3.5 you will need JDK 1.5 or newer to build POI.
Some people may find the tests hang when run through Ant. If this
happens to you, try giving Ant some more memory when you run it, eg:
ANT_OPTS="-Xmx1024m -XX:MaxPermSize=256m" ant test
-->
<project name="POI Build" default="help" basedir="." xmlns:mvn="antlib:org.apache.maven.artifact.ant">
<description>The Apache POI project Ant build.</description>
<property name="version.id" value="3.11-beta1"/>
<property environment="env"/>
<!-- the repository to download jars from -->
<property name="repository.m2" value="http://repo1.maven.org"/>
<property name="main.lib" location="lib"/>
<property name="ooxml.lib" location="ooxml-lib"/>
<property name="forrest.home" value="${env.FORREST_HOME}"/>
<!-- Main: -->
<property name="main.resource1.dir" value="src/resources/main"/>
<property name="main.src" location="src/java"/>
<property name="main.src.test" location="src/testcases"/>
<property name="main.documentation" value="src/documentation"/>
<property name="main.output.dir" location="build/classes"/>
<property name="main.output.test.dir" location="build/test-classes"/>
<property name="main.reports.test" location="build/test-results"/>
<property name="main.testokfile" location="build/main-testokfile.txt"/>
<!-- Examples: -->
<property name="examples.src" location="src/examples/src"/>
<property name="examples.output.dir" location="build/examples-classes"/>
<!-- OOXML support: -->
<property name="ooxml.src" location="src/ooxml/java"/>
<property name="ooxml.resource1.dir" value="src/resources/ooxml"/>
<property name="ooxml.src.test" location="src/ooxml/testcases"/>
<property name="ooxml.reports.test" location="build/ooxml-test-results"/>
<property name="ooxml.output.dir" location="build/ooxml-classes"/>
<property name="ooxml.output.test.dir" location="build/ooxml-test-classes"/>
<property name="ooxml.testokfile" location="build/ooxml-testokfile.txt"/>
<property name="ooxml.lite.output.dir" location="build/ooxml-lite-classes"/>
<property name="ooxml.encryption.xsd.dir" location="src/ooxml/resources/org/apache/poi/poifs/crypt"/>
<!-- coverage libs -->
<property name="jacoco.zip" location="${main.lib}/jacoco-0.6.5.201403032054.zip"/>
<property name="jacoco.url" value="${repository.m2}/maven2/org/jacoco/jacoco/0.6.5.201403032054/jacoco-0.6.5.201403032054.zip"/>
<property name="asm.jar" location="${main.lib}/asm-all-4.0.jar"/>
<property name="asm.url" value="${repository.m2}/maven2/org/ow2/asm/asm-all/4.0/asm-all-4.0.jar"/>
<!-- See http://www.ecma-international.org/publications/standards/Ecma-376.htm -->
<!-- "Copy these file(s), free of charge" -->
<property name="ooxml.xsds.ozip" location="${ooxml.lib}/OfficeOpenXML-Part4.zip"/>
<property name="ooxml.xsds.izip" location="${ooxml.lib}/OfficeOpenXML-XMLSchema.zip"/>
<property name="ooxml.xsds.url"
value="http://www.ecma-international.org/publications/files/ECMA-ST/Office%20Open%20XML%201st%20edition%20Part%204%20(PDF).zip"/>
<property name="ooxml.xsds.src.dir" location="build/ooxml-xsds-src"/>
<property name="ooxml.xsds.src.jar" location="${ooxml.lib}/ooxml-schemas-src-1.1.jar"/>
<property name="ooxml.xsds.jar" location="${ooxml.lib}/ooxml-schemas-1.1.jar"/>
<property name="ooxml.encryption.src.dir" location="build/ooxml-encryption-src"/>
<property name="ooxml.encryption.src.jar" location="${ooxml.lib}/ooxml-encryption-src-1.1.jar"/>
<property name="ooxml.encryption.jar" location="${ooxml.lib}/ooxml-encryption-1.1.jar"/>
<property name="maven.ooxml.xsds.version.id" value="1.0"/>
<property name="maven.ooxml.xsds.jar" value="ooxml-schemas-${maven.ooxml.xsds.version.id}.jar"/>
<!-- Coverage -->
<property name="coverage.dir" value="build/coverage"/>
<!-- Exclude some uninteresting classes from coverage-instrumentation as we do not want to measure coverage in those packages anyway -->
<property name="coverage.excludes" value="org.openxmlformats.*:com.*:org.junit.*:junit.*:"/>
<!-- Apache RAT license check properties -->
<property name="rat.reportdir" value="build/rat"/>
<property name="rat.report" value="${rat.reportdir}/report.txt"/>
<!-- build and distro settings -->
<property name="jar.name" value="poi"/>
<property name="build.site" location="build/tmp/site/build/site"/>
<property name="build.site.src" location="build/tmp/site"/>
<property name="apidocs.report.dir" location="${build.site}/apidocs"/>
<property name="dist.dir" location="build/dist"/>
<property name="halt.on.test.failure" value="true"/>
<propertyset id="junit.properties">
<propertyref name="POI.testdata.path"/>
<propertyref name="java.awt.headless"/>
<propertyref name="org.apache.poi.util.POILogger"/>
</propertyset>
<path id="main.classpath">
<pathelement location="${main.commons-logging.jar}"/>
<pathelement location="${main.commons-codec.jar}"/>
<pathelement location="${main.log4j.jar}"/>
<pathelement location="${main.junit.jar}"/>
<pathelement location="${main.hamcrest.jar}"/>
</path>
<path id="ooxml.classpath">
<pathelement location="${ooxml.jsr173.jar}"/>
<pathelement location="${ooxml.dom4j.jar}"/>
<pathelement location="${ooxml.xmlbeans.jar}"/>
<pathelement location="${ooxml.xsds.jar}"/>
<path refid="main.classpath"/>
<pathelement location="${main.output.dir}"/>
<pathelement location="${ooxml.encryption.jar}"/>
</path>
<path id="test.classpath">
<path refid="main.classpath"/>
<pathelement location="${main.output.dir}"/>
<pathelement location="${main.output.test.dir}"/>
</path>
<path id="test.ooxml.classpath">
<path refid="ooxml.classpath"/>
<pathelement location="${ooxml.output.dir}"/>
<pathelement location="${ooxml.output.test.dir}"/>
<pathelement location="${main.output.test.dir}"/>
</path>
<path id="ooxml-lite.classpath">
<pathelement location="${ooxml.jsr173.jar}"/>
<pathelement location="${ooxml.dom4j.jar}"/>
<pathelement location="${ooxml.xmlbeans.jar}"/>
<pathelement location="build/ooxml-xsds-lite"/> <!-- instead of ooxml-xsds.jar use the filtered classes-->
<path refid="main.classpath"/>
<pathelement location="${main.output.dir}"/>
<path refid="ooxml.classpath"/>
<pathelement location="${ooxml.output.dir}"/>
<pathelement location="${ooxml.output.test.dir}"/>
<pathelement location="${main.output.test.dir}"/>
</path>
<path id="examples.classpath">
<path refid="main.classpath"/>
<pathelement location="${main.output.dir}"/>
</path>
<path id="lib.jacoco">
<fileset dir="lib">
<include name="org.jacoco*.jar" />
<include name="asm-all-4*.jar" />
</fileset>
</path>
<!-- Prints POI's Ant usage help -->
<target name="help" description="Prints Apache POI's Ant usage help">
<echo>
This is POI ${version.id}
Java Version ${ant.java.version}
Timestamp ${DSTAMP}
The main targets of interest are:
- clean Erase all build work products (ie. everything in the build directory)
- compile Compile all files from main, ooxml
- test Run all unit tests from main, ooxml
- jar Produce jar files
- site Generate all documentation (Requires Apache Forrest)
- dist Create a distribution (Requires Apache Forrest)
</echo>
</target>
<tstamp>
<format property="tstamp.year" pattern="yyyy"/>
</tstamp>
<target name="init" depends="fetch-jars,fetch-ooxml-jars">
<mkdir dir="build"/>
<mkdir dir="${main.output.dir}"/>
<mkdir dir="${main.output.test.dir}"/>
<mkdir dir="${main.reports.test}"/>
<mkdir dir="${ooxml.output.dir}"/>
<mkdir dir="${ooxml.output.test.dir}"/>
<mkdir dir="${ooxml.reports.test}"/>
<mkdir dir="${examples.output.dir}"/>
<mkdir dir="${dist.dir}"/>
<mkdir dir="${build.site}"/>
</target>
<target name="clean">
<delete dir="build"/>
</target>
<target name="testexist">
<echo message="Testing for ${destfile}" level="debug"/>
<available file="${destfile}" property="exist"/>
</target>
<target name="downloadfile" unless="exist" depends="testexist">
<get src="${sourcefile}" dest="${destfile}"/>
</target>
<target name="check-jars">
<condition property="jars.present">
<or>
<and>
<available file="${main.commons-logging.jar}"/>
<available file="${main.commons-codec.jar}"/>
<available file="${main.log4j.jar}"/>
<available file="${main.junit.jar}"/>
<available file="${main.hamcrest.jar}"/>
<available file="${main.ant.jar}"/>
<available file="${asm.jar}"/>
<available file="${jacoco.zip}"/>
<available file="${rat.jar}"/>
</and>
<isset property="disconnected"/>
</or>
</condition>
</target>
<target name="fetch-jars" depends="check-jars" unless="jars.present"
description="Fetches needed JAR files from the Internet">
<mkdir dir="${main.lib}"/>
<antcall target="downloadfile">
<param name="sourcefile" value="${main.commons-logging.url}"/>
<param name="destfile" value="${main.commons-logging.jar}"/>
</antcall>
<antcall target="downloadfile">
<param name="sourcefile" value="${main.commons-codec.url}"/>
<param name="destfile" value="${main.commons-codec.jar}"/>
</antcall>
<antcall target="downloadfile">
<param name="sourcefile" value="${main.log4j.url}"/>
<param name="destfile" value="${main.log4j.jar}"/>
</antcall>
<antcall target="downloadfile">
<param name="sourcefile" value="${main.junit.url}"/>
<param name="destfile" value="${main.junit.jar}"/>
</antcall>
<antcall target="downloadfile">
<param name="sourcefile" value="${main.hamcrest.url}"/>
<param name="destfile" value="${main.hamcrest.jar}"/>
</antcall>
<antcall target="downloadfile">
<param name="sourcefile" value="${main.ant.url}"/>
<param name="destfile" value="${main.ant.jar}"/>
</antcall>
<antcall target="downloadfile">
<param name="sourcefile" value="${asm.url}"/>
<param name="destfile" value="${asm.jar}"/>
</antcall>
<antcall target="downloadfile">
<param name="sourcefile" value="${jacoco.url}"/>
<param name="destfile" value="${jacoco.zip}"/>
</antcall>
<unzip src="${jacoco.zip}" dest=".">
<patternset>
<include name="lib/*.jar"/>
</patternset>
</unzip>
<antcall target="downloadfile">
<param name="sourcefile" value="${rat.url}"/>
<param name="destfile" value="${rat.jar}"/>
</antcall>
</target>
<target name="check-ooxml-jars">
<condition property="ooxml.jars.present">
<or>
<and>
<available file="${ooxml.dom4j.jar}"/>
<available file="${ooxml.xmlbeans.jar}"/>
<available file="${ooxml.jsr173.jar}"/>
<available file="${ooxml.xsds.jar}"/>
</and>
<isset property="disconnected"/>
</or>
</condition>
</target>
<target name="fetch-ooxml-jars" depends="check-ooxml-jars" unless="ooxml.jars.present">
<mkdir dir="${ooxml.lib}"/>
<antcall target="downloadfile">
<param name="sourcefile" value="${ooxml.dom4j.url}"/>
<param name="destfile" value="${ooxml.dom4j.jar}"/>
</antcall>
<antcall target="downloadfile">
<param name="sourcefile" value="${ooxml.xmlbeans.url}"/>
<param name="destfile" value="${ooxml.xmlbeans.jar}"/>
</antcall>
<antcall target="downloadfile">
<param name="sourcefile" value="${ooxml.jsr173.url}"/>
<param name="destfile" value="${ooxml.jsr173.jar}"/>
</antcall>
</target>
<target name="check-ooxml-xsds">
<condition property="ooxml-xsds.present">
<or>
<and>
<available file="${ooxml.xsds.izip}"/>
</and>
<isset property="disconnected"/>
</or>
</condition>
</target>
<target name="fetch-ooxml-xsds" unless="ooxml-xsds.present"
depends="check-ooxml-xsds"
description="Fetches needed OOXML xsd files from the Internet">
<get src="${ooxml.xsds.url}" dest="${ooxml.xsds.ozip}"/>
<unzip src="${ooxml.xsds.ozip}" dest="${ooxml.lib}">
<patternset>
<include name="OfficeOpenXML-XMLSchema.zip"/>
</patternset>
</unzip>
</target>
<target name="check-compiled-ooxml-xsds" depends="fetch-ooxml-xsds">
<condition property="ooxml-compiled-xsds.present">
<or>
<available file="${ooxml.xsds.jar}"/>
<isset property="disconnected"/>
</or>
</condition>
<condition property="ooxml-compiled-encryption-xsds.present">
<or>
<available file="${ooxml.encryption.jar}"/>
<isset property="disconnected"/>
</or>
</condition>
</target>
<target name="compile-ooxml-xsds" unless="ooxml-compiled-xsds.present"
depends="check-jars,fetch-jars,check-compiled-ooxml-xsds"
description="Unpacks the OOXML xsd files, and compiles them into XmlBeans">
<property name="ooxml.xsds.tmp.dir" location="build/ooxml-xsds"/>
<mkdir dir="${ooxml.xsds.tmp.dir}"/>
<taskdef name="xmlbean"
classname="org.apache.xmlbeans.impl.tool.XMLBean"
classpath="${ooxml.xmlbeans.jar}:${ooxml.jsr173.jar}"/>
<!-- We need a fair amount of memory to compile the xml schema, -->
<!-- but limit it in case it goes wrong! -->
<!-- Pick the right amount based on 32 vs 64 bit jvm -->
<condition property="ooxml.memory" value="768m" else="512m">
<equals arg1="${sun.arch.data.model}" arg2="64" />
</condition>
<unzip src="${ooxml.xsds.izip}" dest="${ooxml.xsds.tmp.dir}"/>
<!--
schema="build/ooxml-xsds/"
schema="build/ooxml-xsds/sml-workbook.xsd"
-->
<xmlbean
schema="${ooxml.xsds.tmp.dir}"
srcgendir="${ooxml.xsds.src.dir}"
optimize="yes"
destfile="${ooxml.xsds.jar}"
javasource="1.5"
failonerror="true"
fork="true"
memoryMaximumSize="${ooxml.memory}"
>
<classpath refid="ooxml.classpath"/>
</xmlbean>
<!-- Now make a jar of the schema sources -->
<jar
basedir="${ooxml.xsds.src.dir}"
destfile="${ooxml.xsds.src.jar}"
/>
</target>
<target name="compile-ooxml-encryption-xsds" unless="ooxml-compiled-encryption-xsds.present"
depends="check-jars,fetch-jars,check-compiled-ooxml-xsds"
description="Compiles the OOXML encryption xsd files into XmlBeans">
<taskdef name="xmlbean"
classname="org.apache.xmlbeans.impl.tool.XMLBean"
classpath="${ooxml.xmlbeans.jar}:${ooxml.jsr173.jar}"/>
<!-- We need a fair amount of memory to compile the xml schema, -->
<!-- but limit it in case it goes wrong! -->
<!-- Pick the right amount based on 32 vs 64 bit jvm -->
<condition property="ooxml.memory" value="768m" else="512m">
<equals arg1="${sun.arch.data.model}" arg2="64" />
</condition>
<xmlbean
schema="${ooxml.encryption.xsd.dir}"
srcgendir="${ooxml.encryption.src.dir}"
optimize="yes"
destfile="${ooxml.encryption.jar}"
javasource="1.5"
failonerror="true"
fork="true"
memoryMaximumSize="${ooxml.memory}"
>
<classpath refid="ooxml.classpath"/>
</xmlbean>
<!-- Now make a jar of the schema sources -->
<jar
basedir="${ooxml.encryption.src.dir}"
destfile="${ooxml.encryption.src.jar}"
/>
</target>
<target name="compile" depends="init, compile-main,
compile-examples"
description="Compiles the POI main classes and examples"/>
<target name="compile-all" depends="compile,compile-ooxml-lite"/>
<target name="compile-main" depends="init">
<javac target="${jdk.version.class}"
source="${jdk.version.source}"
destdir="${main.output.dir}"
srcdir="${main.src}"
debug="${compile.debug}"
encoding="${java.source.encoding}"
fork="yes"
includeantruntime="false">
<classpath refid="main.classpath"/>
</javac>
<javac target="${jdk.version.class}"
source="${jdk.version.source}"
destdir="${main.output.test.dir}"
srcdir="${main.src.test}"
debug="${compile.debug}"
encoding="${java.source.encoding}"
fork="yes"
includeantruntime="false">
<classpath>
<path refid="main.classpath"/>
<pathelement path="${main.output.dir}"/>
</classpath>
</javac>
<copy todir="${main.output.dir}">
<fileset dir="${main.resource1.dir}"/>
</copy>
</target>
<target name="compile-ooxml" depends="compile-main,compile-ooxml-xsds,compile-ooxml-encryption-xsds">
<javac target="${jdk.version.class}"
source="${jdk.version.source}"
destdir="${ooxml.output.dir}"
srcdir="${ooxml.src}"
debug="${compile.debug}"
encoding="${java.source.encoding}"
fork="yes"
includeantruntime="false">
<classpath refid="ooxml.classpath"/>
</javac>
<javac target="${jdk.version.class}"
source="${jdk.version.source}"
destdir="${ooxml.output.test.dir}"
srcdir="${ooxml.src.test}"
debug="${compile.debug}"
encoding="${java.source.encoding}"
fork="yes"
includeantruntime="false">
<classpath>
<path refid="ooxml.classpath"/>
<pathelement path="${ooxml.output.dir}"/>
<pathelement path="${main.output.test.dir}"/>
</classpath>
</javac>
<copy todir="${ooxml.output.dir}">
<fileset dir="${ooxml.resource1.dir}"/>
</copy>
</target>
<target name="compile-version" depends="init"
description="Compiles the version class">
<!-- Generate the .java file -->
<property name="version.java" value="${main.output.dir}/org/apache/poi/Version.java"/>
<delete file="${version.java}"/>
<copy
file="src/resources/version/Version.java.template"
tofile="${version.java}">
<filterset>
<filter token="VERSION" value="${version.id}"/>
<filter token="DSTAMP" value="${DSTAMP}"/>
</filterset>
</copy>
<!-- Compile -->
<javac target="${jdk.version.class}" source="${jdk.version.source}"
failonerror="true" destdir="${main.output.dir}" debug="on" fork="yes"
srcdir="${main.output.dir}"
encoding="${java.source.encoding}" includeantruntime="false">
</javac>
<!-- Tidy up -->
<delete file="${version.java}"/>
</target>
<target name="jacocotask" depends="">
<echo message="Coverage reporting: ${coverage.enabled}"/>
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
<classpath refid="lib.jacoco"/>
</taskdef>
</target>
<target name="test" depends="compile,jacocotask,test-main,test-ooxml"
description="Tests main and ooxml"/>
<target name="test-all" depends="test,test-ooxml-lite,testcoveragereport"/>
<target name="testcoveragereport" depends="jacocotask" description="create test-report" xmlns:jacoco="antlib:org.jacoco.ant" if="coverage.enabled">
<delete dir="${coverage.dir}"/>
<mkdir dir="${coverage.dir}"/>
<jacoco:report>
<executiondata>
<fileset dir="build">
<include name="*.exec"/>
</fileset>
</executiondata>
<structure name="Apache POI">
<group name="Main">
<classfiles>
<fileset dir="${main.output.dir}">
<exclude name="org/apache/poi/hssf/usermodel/DummyGraphics2d.class"/>
</fileset>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${main.src}"/>
</sourcefiles>
</group>
<group name="Scratchpad">
<classfiles>
<fileset dir="${scratchpad.output.dir}">
<exclude name="org/apache/poi/hwpf/model/types/*.class"/>
<exclude name="org/apache/poi/hdf/model/hdftypes/definitions/*.class"/>
</fileset>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${scratchpad.src}"/>
</sourcefiles>
</group>
<group name="OOXML">
<classfiles>
<fileset dir="${ooxml.output.dir}">
<exclude name=""/>
</fileset>
<fileset dir="${ooxml.lite.output.dir}">
<exclude name="org/openxmlformats/**"/>
<exclude name="schema*/**"/>
</fileset>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${ooxml.src}"/>
<!--fileset dir="${ooxml.lite.src}"/-->
</sourcefiles>
</group>
<group name="Excelant">
<classfiles>
<fileset dir="${excelant.output.dir}"/>
</classfiles>
<sourcefiles encoding="UTF-8">
<fileset dir="${excelant.src}"/>
</sourcefiles>
</group>
</structure>
<html destdir="${coverage.dir}"/>
<xml destfile="${coverage.dir}/coverage.xml"/>
</jacoco:report>
<echo message="Coverage results are available at coverage\index.html, coverage/coverage.xml" />
</target>
<target name="-test-main-check">
<uptodate property="main.test.notRequired" targetfile="${main.testokfile}">
<srcfiles dir="${main.src}"/>
<srcfiles dir="${main.src.test}"/>
</uptodate>
</target>
<target name="test-main" unless="main.test.notRequired"
depends="compile-main, -test-main-check,jacocotask" xmlns:jacoco="antlib:org.jacoco.ant">
<jacoco:coverage enabled="${coverage.enabled}" excludes="${coverage.excludes}" destfile="build/jacoco-main.exec">
<junit fork="yes" forkmode="once" printsummary="yes" haltonfailure="${halt.on.test.failure}"
failureproperty="main.test.failed" showoutput="true">
<classpath refid="test.classpath"/>
<syspropertyset refid="junit.properties"/>
<jvmarg value="${poi.test.locale}"/>
<jvmarg value="-ea"/>
<jvmarg value="-Xmx256m"/>
<formatter type="plain"/>
<formatter type="xml"/>
<batchtest todir="${main.reports.test}">
<fileset dir="${main.src.test}">
<include name="**/${testpattern}.java"/>
<exclude name="**/All*Tests.java"/>
<exclude name="**/TestUnfixedBugs.java"/>
<exclude name="**/TestcaseRecordInputStream.java"/>
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
<delete file="${main.testokfile}"/>
<antcall target="-test-main-write-testfile"/>
</target>
<target name="-test-property-check" unless="testcase">
<echo message="Please use -Dtestcase=org.your.testcase to run a single test"/>
<fail/>
</target>
<target name="-test-main-write-testfile" unless="main.test.failed">
<echo file="${main.testokfile}" append="false" message="testok"/>
</target>
<target name="-test-ooxml-check">
<uptodate property="ooxml.test.notRequired" targetfile="${ooxml.testokfile}">
<srcfiles dir="${ooxml.src}"/>
<srcfiles dir="${ooxml.src.test}"/>
</uptodate>
</target>
<macrodef name="ooxml-test-runner" xmlns:jacoco="antlib:org.jacoco.ant">
<attribute name="classpath"/>
<attribute name="type"/>
<sequential>
<jacoco:coverage enabled="${coverage.enabled}" excludes="${coverage.excludes}" destfile="build/jacoco-@{type}.exec">
<junit printsummary="yes" fork="yes" forkmode="once" haltonfailure="${halt.on.test.failure}"
failureproperty="ooxml.test.failed">
<classpath refid="@{classpath}"/>
<syspropertyset refid="junit.properties"/>
<jvmarg value="${poi.test.locale}"/>
<jvmarg value="-ea"/>
<formatter type="plain"/>
<formatter type="xml"/>
<batchtest todir="${ooxml.reports.test}">
<fileset dir="${ooxml.src.test}">
<include name="**/${testpattern}.java"/>
<exclude name="**/TestUnfixedBugs.java"/>
<exclude name="**/All*Tests.java"/>
</fileset>
</batchtest>
</junit>
</jacoco:coverage>
</sequential>
</macrodef>
<target name="test-ooxml" depends="compile-main,compile-ooxml,-test-ooxml-check,jacocotask" unless="ooxml.test.notRequired">
<ooxml-test-runner classpath="test.ooxml.classpath" type="ooxml"/>
<delete file="${ooxml.testokfile}"/>
<antcall target="-test-ooxml-write-testfile"/>
</target>
<target name="-test-ooxml-write-testfile" unless="ooxml.test.failed">
<echo file="${ooxml.testokfile}" append="false" message="testok"/>
</target>
<target name="compile-ooxml-lite" depends="compile-ooxml">
<property name="ooxml.lite-merged.dir" location="build/ooxml-lite-merged"/>
<mkdir dir="${ooxml.lite-merged.dir}"/>
<jar destfile="${ooxml.lite-merged.dir}/ooxml-lite-merged.jar">
<zipfileset includes="**/*" src="${ooxml.xsds.jar}"/>
<zipfileset includes="**/*" src="${ooxml.encryption.jar}"/>
</jar>
<java classname="org.apache.poi.util.OOXMLLite" fork="yes">
<classpath>
<pathelement path="${ooxml.lite-merged.dir}/ooxml-lite-merged.jar"/>
</classpath>
<classpath refid="test.ooxml.classpath"/>
<syspropertyset refid="junit.properties"/>
<jvmarg value="${poi.test.locale}"/>
<arg value="-ooxml"/>
<arg value="${ooxml.lite-merged.dir}/ooxml-lite-merged.jar"/>
<arg value="-test"/>
<arg value="${ooxml.output.test.dir}"/>
<arg value="-dest"/>
<arg value="${ooxml.lite.output.dir}"/>
</java>
</target>
<target name="test-ooxml-lite" depends="jacocotask">
<delete file="${ooxml.testokfile}"/>
<echo message="Running ooxml tests against 'poi-ooxml-schemas'"/>
<ooxml-test-runner classpath="ooxml-lite.classpath" type="ooxml-lite"/>
</target>
<target name="-check-docs">
<uptodate property="main.docs.notRequired" targetfile="${build.site}/index.html">
<srcfiles dir="${main.documentation}" />
</uptodate>
</target>
<target name="-check-forrest-installed" unless="env.FORREST_HOME">
<echo>Please install Apache Forrest (see
&lt;http://forrest.apache.org/index.html&gt;) and set the
FORREST_HOME environment variable!
</echo>
<fail message="Apache Forrest is not installed."/>
</target>
<target name="docs" depends="init, -check-forrest-installed, -check-docs"
unless="main.docs.notRequired" description="Builds the POI website">
<mkdir dir="${build.site.src}/${main.documentation}"/>
<copy todir="${build.site.src}/${main.documentation}">
<fileset dir="${main.documentation}"/>
</copy>
<copy file="forrest.properties" tofile="${build.site.src}/forrest.properties"/>
<move
file="${build.site.src}/src/documentation/content/xdocs/status.xml"
tofile="${build.site.src}/status.xml"/>
<ant antfile="${env.FORREST_HOME}/forrest.antproxy.xml" target="site">
<property name="project.home" location="${build.site.src}"/>
</ant>
<echo>Broken links:</echo>
<echo file="${build.site}/../tmp/brokenlinks.txt"/>
<touch>
<fileset dir="${build.site}"/>
</touch>
</target>
<!-- Generates documentation and reports -->
<target name="site" depends="docs"
description="Generates POI's website's contents"/>
<!-- TODO to be done with assembly -->
<target name="assemble" depends="jar,jar-src">
<!-- jars to include in binary assemblies -->
<patternset id="bin.dist.jars">
<include name="${jar.name}-${version.id}-${DSTAMP}.jar"/>
<include name="${jar.name}-ooxml-${version.id}-${DSTAMP}.jar"/>
<include name="${jar.name}-examples-${version.id}-${DSTAMP}.jar"/>
<include name="${jar.name}-ooxml-schemas-${version.id}-${DSTAMP}.jar"/>
</patternset>
<!-- patterns to exclude from source assemblies -->
<patternset id="src.dist.patterns"
excludes="build/**,
maven/**,
lib/**,
ooxml-lib/**,
scripts/**,
TEST*,
*.ipr,
*.iml,
*.iws,
*.lnk,
*.rdf,
*.swp,
.settings/**,
.classpath,
.settings/**,
.project"/>
<property name="zipdir" value="${jar.name}-${version.id}"/>
<zip destfile="${dist.dir}/${jar.name}-bin-${version.id}-${DSTAMP}.zip">
<zipfileset dir="legal/" prefix="${zipdir}"/>
<zipfileset dir="${main.lib}" prefix="${zipdir}/lib">
<include name="commons-codec-*.jar"/>
<include name="commons-logging-*.jar"/>
<include name="junit-*.jar"/>
<include name="log4j-*.jar"/>
</zipfileset>
<zipfileset dir="${ooxml.lib}" prefix="${zipdir}/ooxml-lib">
<include name="dom4j-*.jar"/>
<include name="stax-api-*.jar"/>
<include name="xmlbeans-*.jar"/>
</zipfileset>
<zipfileset dir="${dist.dir}" prefix="${zipdir}">
<patternset refid="bin.dist.jars"/>
</zipfileset>
<zipfileset dir="${build.site}" prefix="${zipdir}/docs"/>
</zip>
<zip destfile="${dist.dir}/${jar.name}-src-${version.id}-${DSTAMP}.zip">
<zipfileset dir="legal/" prefix="${zipdir}" />
<zipfileset dir="${build.site}" prefix="${zipdir}/docs"/>
<zipfileset dir="." prefix="${zipdir}">
<patternset refid="src.dist.patterns"/>
</zipfileset>
</zip>
<tar destfile="${dist.dir}/${jar.name}-bin-${version.id}-${DSTAMP}.tar.gz"
longfile="gnu"
compression="gzip">
<tarfileset dir="legal/" prefix="${zipdir}"/>
<zipfileset dir="${main.lib}" prefix="${zipdir}/lib">
<include name="commons-codec-*.jar"/>
<include name="commons-logging-*.jar"/>
<include name="junit-*.jar"/>
<include name="log4j-*.jar"/>
</zipfileset>
<tarfileset dir="${ooxml.lib}" prefix="${zipdir}/ooxml-lib">
<include name="dom4j-*.jar"/>
<include name="stax-api-*.jar"/>
<include name="xmlbeans-*.jar"/>
</tarfileset>
<tarfileset dir="${build.site}" prefix="${zipdir}/docs"/>
<tarfileset dir="${dist.dir}" prefix="${zipdir}">
<patternset refid="bin.dist.jars"/>
</tarfileset>
</tar>
<tar destfile="${dist.dir}/${jar.name}-src-${version.id}-${DSTAMP}.tar.gz"
longfile="gnu"
compression="gzip">
<tarfileset dir="legal/" prefix="${zipdir}" />
<tarfileset dir="${build.site}" prefix="${zipdir}/docs"/>
<tarfileset dir="." prefix="${zipdir}">
<patternset refid="src.dist.patterns"/>
</tarfileset>
</tar>
<!-- script to create signatures and hashes -->
<copy file="maven/multisign.sh" todir="${dist.dir}"/>
<echo>Creating Maven POMs</echo>
<antcall target="maven-poms"/>
<echo>Distribution located in ${dist.dir}</echo>
<echo>Use ${dist.dir}/multisign.sh to create md5 checksums and GPG signatures</echo>
</target>
<target name="dist" depends="clean, compile-all, test-all, site, jar, assemble"
description="Creates the entire distribution into build/dist, from scratch">
</target>
<target name="gump" depends="compile-all, test-all, jar"/>
<target name="jenkins" depends="compile-all, test-all, jar, assemble, findbugs, rat-check"/>
<available property="maven.ant.tasks.present" classname="org.apache.maven.artifact.ant.Pom"/>
<target name="maven.ant.tasks-check">
<fail unless="maven.ant.tasks.present">
Maven ant tasks not found.
Please make sure the maven-ant-tasks jar is in ANT_HOME/lib, or made
available to Ant using other mechanisms like -lib or CLASSPATH.
You can download the Maven Ant Tasks from http://maven.apache.org/ant-tasks/download.html
</fail>
</target>
<macrodef name="m2-install">
<attribute name="artifactId"/>
<sequential>
<mvn:install file="${dist.dir}/@{artifactId}-${version.id}-${DSTAMP}.jar">
<pom file="${dist.dir}/@{artifactId}-${version.id}.pom"/>
</mvn:install>
</sequential>
</macrodef>
<!-- Runs Apache Rat against the source code, to spot any files -->
<!-- which are missing the correct license headers -->
<!-- You need to download rat from http://incubator.apache.org/rat/ -->
<!-- and place the Rat jar into your ant lib before running -->
<target name="rat-check" depends="check-jars,fetch-jars">
<mkdir dir="${rat.reportdir}" />
<typedef resource="org/apache/rat/anttasks/antlib.xml"
uri="antlib:org.apache.rat.anttasks"
classpath="${main.lib}/apache-rat-0.10.jar" />
<rat:report xmlns:rat="antlib:org.apache.rat.anttasks" reportFile="${rat.report}">
<fileset dir="src/">
<exclude name="documentation/content/xdocs/dtd/" />
<exclude name="documentation/content/xdocs/entity/" />
<exclude name="contrib/testcases/dummy.txt" />
<exclude name="examples/lib/dummy.txt" />
<exclude name="resources/ooxml/org/apache/poi/xslf/usermodel/presetShapeDefinitions.xml" />
<exclude name="examples/src/org/apache/poi/xslf/usermodel/pie-chart-data.txt" />
</fileset>
</rat:report>
<loadfile property="rat.reportcontent" srcFile="${rat.report}"/>
<echo>${rat.reportcontent}</echo>
<!-- fail the build if at least one note is in the report -->
<fail><condition><matches pattern="[1-9][0-9]* Unknown Licens" string="${rat.reportcontent}"/></condition></fail>
</target>
</project>

View File

@ -0,0 +1,132 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11-beta1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schema-encryption</artifactId>
<artifactId>poi-ooxml-schema-encryption</artifactId>
<packaging>jar</packaging>
<name>Apach POI - Openxmlformats Encryption Schema package</name>
<properties>
<!-- see http://docs.codehaus.org/display/SONAR/Narrowing+the+Focus for details of this property -->
<sonar.exclusions>target/generated-sources/*</sonar.exclusions>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xmlbeans-maven-plugin</artifactId>
<version>2.3.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>xmlbeans</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>../../src/ooxml/resources/org/apache/poi/poifs/crypt</schemaDirectory>
<javaSource>1.5</javaSource>
<optimize>yes</optimize>
</configuration>
</plugin>
<!-- TODO: ugly workaround as XMLBeans in Maven creates slightly different source compared to the Ant XMLBeans task!?!
see http://stackoverflow.com/questions/21796000/xmlbeans-creates-different-code-when-running-via-ant-and-maven
-->
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<!-- Note: There is a bug with version 1.5.2 which caused the replacement to not find any files sometimes! -->
<version>1.5.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>target/generated-sources/xmlbeans/com/microsoft/schemas/office/x2006/encryption/CTKeyEncryptor.java</include>
<include>target/generated-sources/xmlbeans/com/microsoft/schemas/office/x2006/encryption/impl/CTKeyEncryptorImpl.java</include>
<include>target/generated-sources/xmlbeans/com/microsoft/schemas/office/x2006/keyEncryptor/password/impl/EncryptedKeyDocumentImpl.java</include>
<include>target/generated-sources/xmlbeans/com/microsoft/schemas/office/x2006/keyEncryptor/certificate/EncryptedKeyDocument.java</include>
<include>target/generated-sources/xmlbeans/com/microsoft/schemas/office/x2006/keyEncryptor/certificate/impl/EncryptedKeyDocumentImpl.java</include>
<include>target/generated-sources/xmlbeans/com/microsoft/schemas/office/x2006/keyEncryptor/password/EncryptedKeyDocument.java</include>
<include>target/generated-sources/xmlbeans/com/microsoft/schemas/office/x2006/keyEncryptor/password/impl/EncryptedKeyDocumentImpl.java</include>
</includes>
<replacements>
<replacement>
<token>etEncryptedKey2</token>
<value>etEncryptedCertificateKey</value>
</replacement>
<replacement>
<token>etEncryptedKey</token>
<value>etEncryptedPasswordKey</value>
</replacement>
<replacement>
<token>ewEncryptedKey2</token>
<value>ewEncryptedCertificateKey</value>
</replacement>
<replacement>
<token>ewEncryptedKey</token>
<value>ewEncryptedPasswordKey</value>
</replacement>
<replacement>
<token>encryptedKey2\)</token>
<value>encryptedCertificateKey)</value>
</replacement>
<replacement>
<token>encryptedKey\)</token>
<value>encryptedPasswordKey)</value>
</replacement>
</replacements>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
====================================================================
-->
<xs:schema xmlns="http://schemas.microsoft.com/office/2006/keyEncryptor/certificate" xmlns:e="http://schemas.microsoft.com/office/2006/encryption" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.microsoft.com/office/2006/keyEncryptor/certificate" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="http://schemas.microsoft.com/office/2006/encryption" schemaLocation="encryptionInfo.xsd"/>
<xs:simpleType name="ST_PasswordKeyEncryptorUri">
<xs:restriction base="xs:token">
<xs:enumeration value="http://schemas.microsoft.com/office/2006/keyEncryptor/certificate"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="CT_CertificateKeyEncryptor">
<xs:attribute name="encryptedKeyValue" type="xs:base64Binary" use="required">
<xs:annotation><xs:documentation>A base64-encoded value that specifies the encrypted form of the intermediate key, which is encrypted with the public key contained within the X509Certificate attribute.</xs:documentation></xs:annotation>
</xs:attribute>
<xs:attribute name="X509Certificate" type="xs:base64Binary" use="required">
<xs:annotation><xs:documentation>A base64-encoded value that specifies a DER-encoded X.509 certificate (1) used to encrypt the intermediate key. The certificate (1) MUST contain only the public portion of the public-private key pair.</xs:documentation></xs:annotation>
</xs:attribute>
<xs:attribute name="certVerifier" type="xs:base64Binary" use="required">
<xs:annotation><xs:documentation>A base64-encoded value that specifies the HMAC of the binary data obtained by base64-decoding the X509Certificate attribute. The hashing algorithm used to derive the HMAC MUST be the hashing algorithm specified for the Encryption.keyData element. The secret key used to derive the HMAC MUST be the intermediate key. If the intermediate key is reset, any CertificateKeyEncryptor elements are also reset to contain the new intermediate key, except that the certVerifier attribute MUST match the value calculated using the current intermediate key, to verify that the CertificateKeyEncryptor element actually encrypted the current intermediate key. If a CertificateKeyEncryptor element does not have a correct certVerifier attribute, it MUST be discarded.</xs:documentation></xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:element name="encryptedKey" type="CT_CertificateKeyEncryptor"/>
</xs:schema>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
====================================================================
-->
<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config" xmlns:c="http://schemas.microsoft.com/office/2006/keyEncryptor/certificate">
<xb:qname name="c:encryptedKey" javaname="EncryptedCertificateKey"/>
</xb:config>

View File

@ -0,0 +1,259 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
====================================================================
-->
<xs:schema xmlns="http://schemas.microsoft.com/office/2006/encryption" xmlns:p="http://schemas.microsoft.com/office/2006/keyEncryptor/password" xmlns:c="http://schemas.microsoft.com/office/2006/keyEncryptor/certificate" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.microsoft.com/office/2006/encryption" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="http://schemas.microsoft.com/office/2006/keyEncryptor/password" schemaLocation="encryptionPassword.xsd"/>
<xs:import namespace="http://schemas.microsoft.com/office/2006/keyEncryptor/certificate" schemaLocation="encryptionCertificate.xsd"/>
<xs:simpleType name="ST_SaltSize">
<xs:annotation>
<xs:documentation>An unsigned integer that specifies the number of bytes used by a salt. It MUST be at least 1 and no greater than 65,536.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:unsignedInt">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="65536"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ST_BlockSize">
<xs:annotation>
<xs:documentation>An unsigned integer that specifies the number of bytes used to encrypt one block of data. It MUST be at least 2, no greater than 4096, and a multiple of 2.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:unsignedInt">
<xs:minInclusive value="2"/>
<xs:maxInclusive value="4096"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ST_KeyBits">
<xs:annotation>
<xs:documentation>An unsigned integer that specifies the number of bits used by an encryption algorithm. It MUST be at least 8 and a multiple of 8.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:unsignedInt">
<xs:minInclusive value="8"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ST_HashSize">
<xs:annotation>
<xs:documentation>An unsigned integer that specifies the number of bytes used by a hash value. It MUST be at least 1, no greater than 65,536, and the same number of bytes as the hash algorithm emits.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:unsignedInt">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="65536"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ST_SpinCount">
<xs:annotation>
<xs:documentation>An unsigned integer that specifies the number of times to iterate on a hash of a password. It MUST NOT be greater than 10,000,000.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:unsignedInt">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="10000000"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ST_CipherAlgorithm">
<xs:annotation>
<xs:appinfo>modified for poi - list is restricted to given list in [ms-offcrypto]</xs:appinfo>
<xs:documentation>A string that specifies the cipher algorithm. Values that are not defined MAY be used, and a compliant implementation is not required to support all defined values. Any algorithm that can be resolved by name by the underlying operating system can be used for hashing or encryption. Only block algorithms are supported for encryption. AES-128 is the default encryption algorithm, and SHA-1 is the default hashing algorithm if no other algorithms have been configured.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:token">
<xs:enumeration value="AES">
<xs:annotation>
<xs:documentation>MUST conform to the AES algorithm.</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="RC2">
<xs:annotation>
<xs:documentation>MUST conform to the algorithm as specified in [RFC2268] (http://tools.ietf.org/html/rfc2268). The use of RC2 is not recommended. If RC2 is used with a key length of less than 128 bits, documents could interoperate incorrectly across different versions of Windows.</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="RC4">
<xs:annotation>
<xs:documentation>MUST NOT be used.</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="DES">
<xs:annotation>
<xs:documentation>MUST conform to the DES algorithm. The use of DES is not recommended. If DES is used, the key length specified in the KeyBits element is required to be set to 64 for 56-bit encryption, and the key decrypted from encryptedKeyValue of KeyEncryptor is required to include the DES parity bits.</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="DESX">
<xs:annotation>
<xs:documentation>MUST conform to the algorithm as specified in [DRAFT-DESX] (http://tools.ietf.org/html/draft-ietf-ipsec-ciph-desx-00). The use of DESX is not recommended. If DESX is used, documents could interoperate incorrectly across different versions of Windows.</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="3DES">
<xs:annotation>
<xs:documentation>MUST conform to the algorithm as specified in [RFC1851] (http://tools.ietf.org/html/rfc1851). If 3DES or 3DES_112 is used, the key length specified in the KeyBits element is required to be set to 192 for 168-bit encryption and 128 for 112-bit encryption, and the key decrypted from encryptedKeyValue of KeyEncryptor is required to include the DES parity bits.</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="3DES_112">
<xs:annotation>
<xs:documentation>see 3DES</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ST_CipherChaining">
<xs:annotation>
<xs:documentation>A string that specifies the chaining mode used by CipherAlgorithm. For more details about chaining modes, see [BCMO800-38A] (http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf).</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:token">
<xs:enumeration value="ChainingModeCBC">
<xs:annotation>
<xs:documentation>block chaining (CBC)</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="ChainingModeCFB">
<xs:annotation>
<xs:documentation>Cipher feedback chaining (CFB), with an 8-bit window</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ST_HashAlgorithm">
<xs:annotation>
<xs:appinfo>modified for poi - list is restricted to given list in [ms-offcrypto]</xs:appinfo>
<xs:documentation>A string specifying a hashing algorithm. Values that are not defined MAY be used, and a compliant implementation is not required to support all defined values.</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:token">
<xs:enumeration value="SHA1">
<xs:annotation>
<xs:documentation>MUST conform to the algorithm as specified in [RFC4634] (http://tools.ietf.org/html/rfc4634).</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="SHA256">
<xs:annotation>
<xs:documentation>see SHA1</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="SHA384">
<xs:annotation>
<xs:documentation>see SHA1</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="SHA512">
<xs:annotation>
<xs:documentation>see SHA1</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="MD5">
<xs:annotation>
<xs:documentation>MUST conform to MD5.</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="MD4">
<xs:annotation>
<xs:documentation>MUST conform to the algorithm as specified in [RFC1320] (http://tools.ietf.org/html/rfc1320).</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="MD2">
<xs:annotation>
<xs:documentation>MUST conform to the algorithm as specified in [RFC1319] (http://tools.ietf.org/html/rfc1319).</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="RIPEMD-128">
<xs:annotation>
<xs:documentation>MUST conform to the hash functions specified in [ISO/IEC 10118]. (https://en.wikipedia.org/wiki/RIPEMD)</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="RIPEMD-160">
<xs:annotation>
<xs:documentation>see RIPEMD-128 (https://en.wikipedia.org/wiki/RIPEMD)</xs:documentation>
</xs:annotation>
</xs:enumeration>
<xs:enumeration value="WHIRLPOOL">
<xs:annotation>
<xs:documentation>see RIPEMD-128 (https://en.wikipedia.org/wiki/ISO/IEC_10118-3)</xs:documentation>
</xs:annotation>
</xs:enumeration>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="CT_KeyData">
<xs:annotation>
<xs:documentation>A complex type that specifies the encryption used within this element. The saltValue attribute is a base64-encoded binary value that is randomly generated. The number of bytes required to decode the saltValue attribute MUST be equal to the value of the saltSize attribute.</xs:documentation>
</xs:annotation>
<xs:attribute name="saltSize" type="ST_SaltSize" use="required"/>
<xs:attribute name="blockSize" type="ST_BlockSize" use="required"/>
<xs:attribute name="keyBits" type="ST_KeyBits" use="required"/>
<xs:attribute name="hashSize" type="ST_HashSize" use="required"/>
<xs:attribute name="cipherAlgorithm" type="ST_CipherAlgorithm" use="required"/>
<xs:attribute name="cipherChaining" type="ST_CipherChaining" use="required"/>
<xs:attribute name="hashAlgorithm" type="ST_HashAlgorithm" use="required"/>
<xs:attribute name="saltValue" type="xs:base64Binary" use="required"/>
</xs:complexType>
<xs:complexType name="CT_DataIntegrity">
<xs:annotation>
<xs:documentation>A complex type that specifies data used to verify whether the encrypted data passes an integrity check. It MUST be generated using the method specified in section 2.3.4.14 (http://msdn.microsoft.com/en-us/library/dd924068(v=office.12).aspx).</xs:documentation>
</xs:annotation>
<xs:attribute name="encryptedHmacKey" type="xs:base64Binary" use="required">
<xs:annotation>
<xs:documentation>A base64-encoded value that specifies an encrypted key used in calculating the encryptedHmacValue.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="encryptedHmacValue" type="xs:base64Binary" use="required">
<xs:annotation>
<xs:documentation>A base64-encoded value that specifies an HMAC derived from encryptedHmacKey and the encrypted data.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:complexType name="CT_KeyEncryptor">
<xs:annotation>
<xs:appinfo>modified for POI</xs:appinfo>
<xs:documentation>A complex type that specifies the parameters used to encrypt an intermediate key, which is used to perform the final encryption of the document. To ensure extensibility, arbitrary elements can be defined to encrypt the intermediate key. The intermediate key MUST be the same for all KeyEncryptor elements.</xs:documentation>
</xs:annotation>
<xs:choice>
<xs:element ref="p:encryptedKey"/>
<xs:element ref="c:encryptedKey"/>
</xs:choice>
<xs:attribute name="uri">
<xs:annotation>
<xs:appinfo>modified for POI</xs:appinfo>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:token">
<xs:enumeration value="http://schemas.microsoft.com/office/2006/keyEncryptor/password"/>
<xs:enumeration value="http://schemas.microsoft.com/office/2006/keyEncryptor/certificate"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:complexType name="CT_KeyEncryptors">
<xs:annotation>
<xs:documentation>A sequence of KeyEncryptor elements. Exactly one KeyEncryptors element MUST be present, and the KeyEncryptors element MUST contain at least one KeyEncryptor.</xs:documentation>
</xs:annotation>
<xs:sequence>
<xs:element name="keyEncryptor" type="CT_KeyEncryptor" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="CT_Encryption">
<xs:sequence>
<xs:element name="keyData" type="CT_KeyData"/>
<xs:element name="dataIntegrity" type="CT_DataIntegrity">
<xs:annotation>
<xs:appinfo>modified for POI</xs:appinfo>
<xs:documentation>All ECMA-376 documents [ECMA-376] encrypted by Microsoft Office using agile encryption will have a DataIntegrity element present. The schema allows for a DataIntegrity element to not be present because the encryption schema can be used by applications that do not create ECMA-376 documents [ECMA-376].</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="keyEncryptors" type="CT_KeyEncryptors">
<xs:annotation>
<xs:documentation>The KeyEncryptor element, which MUST be used when encrypting password-protected agile encryption documents, is either a PasswordKeyEncryptor or a CertificateKeyEncryptor. Exactly one PasswordKeyEncryptor MUST be present. Zero or more CertificateKeyEncryptor elements are contained within the KeyEncryptors element.</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="encryption" type="CT_Encryption"/>
</xs:schema>

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
====================================================================
-->
<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config" xmlns:c="http://schemas.microsoft.com/office/2006/keyEncryptor/certificate" xmlns:p="http://schemas.microsoft.com/office/2006/keyEncryptor/password">
<xb:qname name="c:encryptedKey" javaname="EncryptedCertificateKey"/>
<xb:qname name="p:encryptedKey" javaname="EncryptedPasswordKey"/>
</xb:config>

View File

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
====================================================================
-->
<xs:schema xmlns="http://schemas.microsoft.com/office/2006/keyEncryptor/password" xmlns:e="http://schemas.microsoft.com/office/2006/encryption" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.microsoft.com/office/2006/keyEncryptor/password" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:import namespace="http://schemas.microsoft.com/office/2006/encryption" schemaLocation="encryptionInfo.xsd"/>
<xs:simpleType name="ST_PasswordKeyEncryptorUri">
<xs:restriction base="xs:token">
<xs:enumeration value="http://schemas.microsoft.com/office/2006/keyEncryptor/password"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name="CT_PasswordKeyEncryptor">
<xs:attribute name="saltSize" type="e:ST_SaltSize" use="required">
<xs:annotation><xs:documentation>A SaltSize that specifies the size of the salt for a PasswordKeyEncryptor.</xs:documentation></xs:annotation>
</xs:attribute>
<xs:attribute name="blockSize" type="e:ST_BlockSize" use="required">
<xs:annotation><xs:documentation>A BlockSize that specifies the block size for a PasswordKeyEncryptor.</xs:documentation></xs:annotation>
</xs:attribute>
<xs:attribute name="keyBits" type="e:ST_KeyBits" use="required">
<xs:annotation><xs:documentation>A KeyBits that specifies the number of bits for a PasswordKeyEncryptor.</xs:documentation></xs:annotation>
</xs:attribute>
<xs:attribute name="hashSize" type="e:ST_HashSize" use="required">
<xs:annotation><xs:documentation>A HashSize that specifies the size of the binary form of the hash for a PasswordKeyEncryptor.</xs:documentation></xs:annotation>
</xs:attribute>
<xs:attribute name="cipherAlgorithm" type="e:ST_CipherAlgorithm" use="required">
<xs:annotation><xs:documentation>A CipherAlgorithm that specifies the cipher algorithm for a PasswordKeyEncryptor. The cipher algorithm specified MUST be the same as the cipher algorithm specified for the Encryption.keyData element.</xs:documentation></xs:annotation>
</xs:attribute>
<xs:attribute name="cipherChaining" type="e:ST_CipherChaining" use="required">
<xs:annotation><xs:documentation>A CipherChaining that specifies the cipher chaining mode for a PasswordKeyEncryptor.</xs:documentation></xs:annotation>
</xs:attribute>
<xs:attribute name="hashAlgorithm" type="e:ST_HashAlgorithm" use="required">
<xs:annotation><xs:documentation>A HashAlgorithm that specifies the hashing algorithm for a PasswordKeyEncryptor. The hashing algorithm specified MUST be the same as the hashing algorithm specified for the Encryption.keyData element.</xs:documentation></xs:annotation>
</xs:attribute>
<xs:attribute name="saltValue" type="xs:base64Binary" use="required">
<xs:annotation><xs:documentation>A base64-encoded binary byte array that specifies the salt value for a PasswordKeyEncryptor. The number of bytes required by the decoded form of this element MUST be saltSize.</xs:documentation></xs:annotation>
</xs:attribute>
<xs:attribute name="spinCount" type="e:ST_SpinCount" use="required">
<xs:annotation><xs:documentation>A SpinCount that specifies the spin count for a PasswordKeyEncryptor.</xs:documentation></xs:annotation>
</xs:attribute>
<xs:attribute name="encryptedVerifierHashInput" type="xs:base64Binary" use="required">
<xs:annotation><xs:documentation>A base64-encoded value that specifies the encrypted verifier hash input for a PasswordKeyEncryptor used in password verification.</xs:documentation></xs:annotation>
</xs:attribute>
<xs:attribute name="encryptedVerifierHashValue" type="xs:base64Binary" use="required">
<xs:annotation><xs:documentation>A base64-encoded value that specifies the encrypted verifier hash value for a PasswordKeyEncryptor used in password verification.</xs:documentation></xs:annotation>
</xs:attribute>
<xs:attribute name="encryptedKeyValue" type="xs:base64Binary" use="required">
<xs:annotation><xs:documentation>A base64-encoded value that specifies the encrypted form of the intermediate key.</xs:documentation></xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:element name="encryptedKey" type="CT_PasswordKeyEncryptor"/>
</xs:schema>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
====================================================================
-->
<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config" xmlns:p="http://schemas.microsoft.com/office/2006/keyEncryptor/password">
<xb:qname name="p:encryptedKey" javaname="EncryptedPasswordKey"/>
</xb:config>

128
ooxml-schemas/pom.xml Normal file
View File

@ -0,0 +1,128 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ooxml-schemas</artifactId>
<packaging>jar</packaging>
<name>Apache POI - ooxml schema package</name>
<description>XmlBeans generated from the Ecma supplied xsds:
http://www.ecma-international.org/publications/files/ECMA-ST/Office%20Open%20XML%20Part%204%20(DOCX).zip</description>
<url>http://poi.apache.org/</url>
<properties>
<!-- see http://docs.codehaus.org/display/SONAR/Narrowing+the+Focus for details of this property -->
<sonar.exclusions>target/generated-sources/*</sonar.exclusions>
</properties>
<build>
<plugins>
<!-- Download and unpack the OfficeOpenXML Schema and use xmlbeans to create classes from the XSDs -->
<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>maven-download-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<id>install-xsds</id>
<phase>generate-sources</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>http://www.ecma-international.org/publications/files/ECMA-ST/Office%20Open%20XML%201st%20edition%20Part%204%20(PDF).zip</url>
<unpack>true</unpack>
<md5>abe6bb6e7799e854934b3c634e8bcf7b</md5>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>unzip-schema</id>
<phase>generate-sources</phase>
<configuration>
<target>
<echo message="unzip schemas" />
<unzip src="target/OfficeOpenXML-XMLSchema.zip" dest="target/schemas/" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xmlbeans-maven-plugin</artifactId>
<version>2.3.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>xmlbeans</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>target/schemas</schemaDirectory>
<javaSource>1.5</javaSource>
<!--<optimize>yes</optimize>-->
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- TODO to check not required to build
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-main</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${project.version}</version>
</dependency>-->
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
</dependency>
</dependencies>
</project>

75
patch.xml Normal file
View File

@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
=======================================================================
Use Apache Ant to generate a patch file.
=======================================================================
-->
<project name="create-patch" default="patchpackage" basedir=".">
<property environment="env"/>
<property name="patch.package" value="patch.tar.gz"/>
<property name="patch.file" value="patch.txt"/>
<condition property="svn.found">
<or>
<available file="svn" filepath="${env.PATH}"/>
<available file="svn.exe" filepath="${env.PATH}"/>
<available file="svn.exe" filepath="${env.Path}"/>
</or>
</condition>
<target name="createpatch">
<fail unless="svn.found" message="You need a version of svn to create the patch"/>
<exec executable="svn" output="${patch.file}">
<arg value="diff"/>
</exec>
</target>
<target name="newfiles">
<exec executable="svn" output="${patch.file}.tmp">
<arg value="status"/>
</exec>
<!-- prepare the list of files to include in patch.tar.gz -->
<loadfile srcfile="${patch.file}.tmp" property="tar.file.list">
<filterchain>
<!-- capture any new files -->
<linecontainsregexp>
<regexp pattern="^(\?|A)......"/>
</linecontainsregexp>
<!-- filter out the first six characters -->
<tokenfilter>
<replaceregex pattern="^(.......)" replace=""/>
</tokenfilter>
<!--remove line breaks -->
<striplinebreaks/>
</filterchain>
</loadfile>
</target>
<target name="patchpackage" depends="createpatch,newfiles">
<delete file="${patch.package}"/>
<tar includes="${tar.file.list}"
destfile="${patch.package}"
basedir="."
compression="gzip" >
</tar>
<delete file="${patch.file}.tmp"/>
</target>
</project>

55
poi-examples/pom.xml Normal file
View File

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>poi-examples</artifactId>
<name>Apache POI - examples package</name>
<url>http://poi.apache.org/</url>
<description>Apache POI Examples</description>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-main</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${project.version}</version>
</dependency>
<!-- non-test dependency for UpdateEmbeddedDoc -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,542 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hpsf.examples;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.poi.hpsf.HPSFRuntimeException;
import org.apache.poi.hpsf.MarkUnsupportedException;
import org.apache.poi.hpsf.MutablePropertySet;
import org.apache.poi.hpsf.NoPropertySetStreamException;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.Util;
import org.apache.poi.hpsf.WritingNotSupportedException;
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.POIFSDocumentPath;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.TempFile;
/**
* <p>This class copies a POI file system to a new file and compares the copy
* with the original.</p>
*
* <p>Property set streams are copied logically, i.e. the application
* establishes a {@link org.apache.poi.hpsf.PropertySet} of an original property
* set, creates a {@link org.apache.poi.hpsf.MutablePropertySet} from the
* {@link org.apache.poi.hpsf.PropertySet} and writes the
* {@link org.apache.poi.hpsf.MutablePropertySet} to the destination POI file
* system. - Streams which are no property set streams are copied bit by
* bit.</p>
*
* <p>The comparison of the POI file systems is done logically. That means that
* the two disk files containing the POI file systems do not need to be
* exactly identical. However, both POI file systems must contain the same
* files, and most of these files must be bitwise identical. Property set
* streams, however, are compared logically: they must have the same sections
* with the same attributs, and the sections must contain the same properties.
* Details like the ordering of the properties do not matter.</p>
*
* @author Rainer Klute <a
* href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
*/
public class CopyCompare
{
/**
* <p>Runs the example program. The application expects one or two
* arguments:</p>
*
* <ol>
*
* <li><p>The first argument is the disk file name of the POI filesystem to
* copy.</p></li>
*
* <li><p>The second argument is optional. If it is given, it is the name of
* a disk file the copy of the POI filesystem will be written to. If it is
* not given, the copy will be written to a temporary file which will be
* deleted at the end of the program.</p></li>
*
* </ol>
*
* @param args Command-line arguments.
* @exception MarkUnsupportedException if a POI document stream does not
* support the mark() operation.
* @exception NoPropertySetStreamException if the application tries to
* create a property set from a POI document stream that is not a property
* set stream.
* @exception IOException if any I/O exception occurs.
* @exception UnsupportedEncodingException if a character encoding is not
* supported.
*/
public static void main(final String[] args)
throws NoPropertySetStreamException, MarkUnsupportedException,
UnsupportedEncodingException, IOException
{
String originalFileName = null;
String copyFileName = null;
/* Check the command-line arguments. */
if (args.length == 1)
{
originalFileName = args[0];
File f = TempFile.createTempFile("CopyOfPOIFileSystem-", ".ole2");
f.deleteOnExit();
copyFileName = f.getAbsolutePath();
}
else if (args.length == 2)
{
originalFileName = args[0];
copyFileName = args[1];
}
else
{
System.err.println("Usage: " + CopyCompare.class.getName() +
"originPOIFS [copyPOIFS]");
System.exit(1);
}
/* Read the origin POIFS using the eventing API. The real work is done
* in the class CopyFile which is registered here as a POIFSReader. */
final POIFSReader r = new POIFSReader();
final CopyFile cf = new CopyFile(copyFileName);
r.registerListener(cf);
r.read(new FileInputStream(originalFileName));
/* Write the new POIFS to disk. */
cf.close();
/* Read all documents from the original POI file system and compare them
* with the equivalent document from the copy. */
final POIFSFileSystem opfs =
new POIFSFileSystem(new FileInputStream(originalFileName));
final POIFSFileSystem cpfs =
new POIFSFileSystem(new FileInputStream(copyFileName));
final DirectoryEntry oRoot = opfs.getRoot();
final DirectoryEntry cRoot = cpfs.getRoot();
final StringBuffer messages = new StringBuffer();
if (equal(oRoot, cRoot, messages))
System.out.println("Equal");
else
System.out.println("Not equal: " + messages.toString());
}
/**
* <p>Compares two {@link DirectoryEntry} instances of a POI file system.
* The directories must contain the same streams with the same names and
* contents.</p>
*
* @param d1 The first directory.
* @param d2 The second directory.
* @param msg The method may append human-readable comparison messages to
* this string buffer.
* @return <code>true</code> if the directories are equal, else
* <code>false</code>.
* @exception MarkUnsupportedException if a POI document stream does not
* support the mark() operation.
* @exception NoPropertySetStreamException if the application tries to
* create a property set from a POI document stream that is not a property
* set stream.
* @throws UnsupportedEncodingException
* @exception IOException if any I/O exception occurs.
*/
private static boolean equal(final DirectoryEntry d1,
final DirectoryEntry d2,
final StringBuffer msg)
throws NoPropertySetStreamException, MarkUnsupportedException,
UnsupportedEncodingException, IOException
{
boolean equal = true;
/* Iterate over d1 and compare each entry with its counterpart in d2. */
for (final Iterator i = d1.getEntries(); equal && i.hasNext();)
{
final Entry e1 = (Entry) i.next();
final String n1 = e1.getName();
Entry e2 = null;
try
{
e2 = d2.getEntry(n1);
}
catch (FileNotFoundException ex)
{
msg.append("Document \"" + e1 + "\" exists, document \"" +
e2 + "\" does not.\n");
equal = false;
break;
}
if (e1.isDirectoryEntry() && e2.isDirectoryEntry())
equal = equal((DirectoryEntry) e1, (DirectoryEntry) e2, msg);
else if (e1.isDocumentEntry() && e2.isDocumentEntry())
equal = equal((DocumentEntry) e1, (DocumentEntry) e2, msg);
else
{
msg.append("One of \"" + e1 + "\" and \"" + e2 + "\" is a " +
"document while the other one is a directory.\n");
equal = false;
}
}
/* Iterate over d2 just to make sure that there are no entries in d2
* that are not in d1. */
for (final Iterator i = d2.getEntries(); equal && i.hasNext();)
{
final Entry e2 = (Entry) i.next();
final String n2 = e2.getName();
Entry e1 = null;
try
{
e1 = d1.getEntry(n2);
}
catch (FileNotFoundException ex)
{
msg.append("Document \"" + e2 + "\" exitsts, document \"" +
e1 + "\" does not.\n");
equal = false;
break;
}
}
return equal;
}
/**
* <p>Compares two {@link DocumentEntry} instances of a POI file system.
* Documents that are not property set streams must be bitwise identical.
* Property set streams must be logically equal.</p>
*
* @param d1 The first document.
* @param d2 The second document.
* @param msg The method may append human-readable comparison messages to
* this string buffer.
* @return <code>true</code> if the documents are equal, else
* <code>false</code>.
* @exception MarkUnsupportedException if a POI document stream does not
* support the mark() operation.
* @exception NoPropertySetStreamException if the application tries to
* create a property set from a POI document stream that is not a property
* set stream.
* @throws UnsupportedEncodingException
* @exception IOException if any I/O exception occurs.
*/
private static boolean equal(final DocumentEntry d1, final DocumentEntry d2,
final StringBuffer msg)
throws NoPropertySetStreamException, MarkUnsupportedException,
UnsupportedEncodingException, IOException
{
boolean equal = true;
final DocumentInputStream dis1 = new DocumentInputStream(d1);
final DocumentInputStream dis2 = new DocumentInputStream(d2);
if (PropertySet.isPropertySetStream(dis1) &&
PropertySet.isPropertySetStream(dis2))
{
final PropertySet ps1 = PropertySetFactory.create(dis1);
final PropertySet ps2 = PropertySetFactory.create(dis2);
equal = ps1.equals(ps2);
if (!equal)
{
msg.append("Property sets are not equal.\n");
return equal;
}
}
else
{
int i1;
int i2;
do
{
i1 = dis1.read();
i2 = dis2.read();
if (i1 != i2)
{
equal = false;
msg.append("Documents are not equal.\n");
break;
}
}
while (equal && i1 == -1);
}
return true;
}
/**
* <p>This class does all the work. Its method {@link
* #processPOIFSReaderEvent(POIFSReaderEvent)} is called for each file in
* the original POI file system. Except for property set streams it copies
* everything unmodified to the destination POI filesystem. Property set
* streams are copied by creating a new {@link PropertySet} from the
* original property set by using the {@link
* MutablePropertySet#MutablePropertySet(PropertySet)} constructor.</p>
*/
static class CopyFile implements POIFSReaderListener
{
String dstName;
OutputStream out;
POIFSFileSystem poiFs;
/**
* <p>The constructor of a {@link CopyFile} instance creates the target
* POIFS. It also stores the name of the file the POIFS will be written
* to once it is complete.</p>
*
* @param dstName The name of the disk file the destination POIFS is to
* be written to.
*/
public CopyFile(final String dstName)
{
this.dstName = dstName;
poiFs = new POIFSFileSystem();
}
/**
* <p>The method is called by POI's eventing API for each file in the
* origin POIFS.</p>
*/
public void processPOIFSReaderEvent(final POIFSReaderEvent event)
{
/* The following declarations are shortcuts for accessing the
* "event" object. */
final POIFSDocumentPath path = event.getPath();
final String name = event.getName();
final DocumentInputStream stream = event.getStream();
Throwable t = null;
try
{
/* Find out whether the current document is a property set
* stream or not. */
if (PropertySet.isPropertySetStream(stream))
{
/* Yes, the current document is a property set stream.
* Let's create a PropertySet instance from it. */
PropertySet ps = null;
try
{
ps = PropertySetFactory.create(stream);
}
catch (NoPropertySetStreamException ex)
{
/* This exception will not be thrown because we already
* checked above. */
}
/* Copy the property set to the destination POI file
* system. */
copy(poiFs, path, name, ps);
}
else
/* No, the current document is not a property set stream. We
* copy it unmodified to the destination POIFS. */
copy(poiFs, event.getPath(), event.getName(), stream);
}
catch (MarkUnsupportedException ex)
{
t = ex;
}
catch (IOException ex)
{
t = ex;
}
catch (WritingNotSupportedException ex)
{
t = ex;
}
/* According to the definition of the processPOIFSReaderEvent method
* we cannot pass checked exceptions to the caller. The following
* lines check whether a checked exception occured and throws an
* unchecked exception. The message of that exception is that of
* the underlying checked exception. */
if (t != null)
{
throw new HPSFRuntimeException
("Could not read file \"" + path + "/" + name +
"\". Reason: " + Util.toString(t));
}
}
/**
* <p>Writes a {@link PropertySet} to a POI filesystem.</p>
*
* @param poiFs The POI filesystem to write to.
* @param path The file's path in the POI filesystem.
* @param name The file's name in the POI filesystem.
* @param ps The property set to write.
* @throws WritingNotSupportedException
* @throws IOException
*/
public void copy(final POIFSFileSystem poiFs,
final POIFSDocumentPath path,
final String name,
final PropertySet ps)
throws WritingNotSupportedException, IOException
{
final DirectoryEntry de = getPath(poiFs, path);
final MutablePropertySet mps = new MutablePropertySet(ps);
de.createDocument(name, mps.toInputStream());
}
/**
* <p>Copies the bytes from a {@link DocumentInputStream} to a new
* stream in a POI filesystem.</p>
*
* @param poiFs The POI filesystem to write to.
* @param path The source document's path.
* @param name The source document's name.
* @param stream The stream containing the source document.
* @throws IOException
*/
public void copy(final POIFSFileSystem poiFs,
final POIFSDocumentPath path,
final String name,
final DocumentInputStream stream) throws IOException
{
final DirectoryEntry de = getPath(poiFs, path);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
int c;
while ((c = stream.read()) != -1)
out.write(c);
stream.close();
out.close();
final InputStream in =
new ByteArrayInputStream(out.toByteArray());
de.createDocument(name, in);
}
/**
* <p>Writes the POI file system to a disk file.</p>
*
* @throws FileNotFoundException
* @throws IOException
*/
public void close() throws FileNotFoundException, IOException
{
out = new FileOutputStream(dstName);
poiFs.writeFilesystem(out);
out.close();
}
/** Contains the directory paths that have already been created in the
* output POI filesystem and maps them to their corresponding
* {@link org.apache.poi.poifs.filesystem.DirectoryNode}s. */
private final Map paths = new HashMap();
/**
* <p>Ensures that the directory hierarchy for a document in a POI
* fileystem is in place. When a document is to be created somewhere in
* a POI filesystem its directory must be created first. This method
* creates all directories between the POI filesystem root and the
* directory the document should belong to which do not yet exist.</p>
*
* <p>Unfortunately POI does not offer a simple method to interrogate
* the POIFS whether a certain child node (file or directory) exists in
* a directory. However, since we always start with an empty POIFS which
* contains the root directory only and since each directory in the
* POIFS is created by this method we can maintain the POIFS's directory
* hierarchy ourselves: The {@link DirectoryEntry} of each directory
* created is stored in a {@link Map}. The directories' path names map
* to the corresponding {@link DirectoryEntry} instances.</p>
*
* @param poiFs The POI filesystem the directory hierarchy is created
* in, if needed.
* @param path The document's path. This method creates those directory
* components of this hierarchy which do not yet exist.
* @return The directory entry of the document path's parent. The caller
* should use this {@link DirectoryEntry} to create documents in it.
*/
public DirectoryEntry getPath(final POIFSFileSystem poiFs,
final POIFSDocumentPath path)
{
try
{
/* Check whether this directory has already been created. */
final String s = path.toString();
DirectoryEntry de = (DirectoryEntry) paths.get(s);
if (de != null)
/* Yes: return the corresponding DirectoryEntry. */
return de;
/* No: We have to create the directory - or return the root's
* DirectoryEntry. */
int l = path.length();
if (l == 0)
/* Get the root directory. It does not have to be created
* since it always exists in a POIFS. */
de = poiFs.getRoot();
else
{
/* Create a subordinate directory. The first step is to
* ensure that the parent directory exists: */
de = getPath(poiFs, path.getParent());
/* Now create the target directory: */
de = de.createDirectory(path.getComponent
(path.length() - 1));
}
paths.put(s, de);
return de;
}
catch (IOException ex)
{
/* This exception will be thrown if the directory already
* exists. However, since we have full control about directory
* creation we can ensure that this will never happen. */
ex.printStackTrace(System.err);
throw new RuntimeException(ex.toString());
/* FIXME (2): Replace the previous line by the following once we
* no longer need JDK 1.3 compatibility. */
// throw new RuntimeException(ex);
}
}
}
}

View File

@ -0,0 +1,194 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hpsf.examples;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import org.apache.poi.hpsf.CustomProperties;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.MarkUnsupportedException;
import org.apache.poi.hpsf.NoPropertySetStreamException;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hpsf.UnexpectedPropertySetTypeException;
import org.apache.poi.hpsf.WritingNotSupportedException;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* <p>This is a sample application showing how to easily modify properties in
* the summary information and in the document summary information. The
* application reads the name of a POI filesystem from the command line and
* performs the following actions:</p>
*
* <ul>
*
* <li><p>Open the POI filesystem.</p></li>
*
* <li><p>Read the summary information.</p></li>
*
* <li><p>Read and print the "author" property.</p></li>
*
* <li><p>Change the author to "Rainer Klute".</p></li>
*
* <li><p>Read the document summary information.</p></li>
*
* <li><p>Read and print the "category" property.</p></li>
*
* <li><p>Change the category to "POI example".</p></li>
*
* <li><p>Read the custom properties (if available).</p></li>
*
* <li><p>Insert a new custom property.</p></li>
*
* <li><p>Write the custom properties back to the document summary
* information.</p></li>
*
* <li><p>Write the summary information to the POI filesystem.</p></li>
*
* <li><p>Write the document summary information to the POI filesystem.</p></li>
*
* <li><p>Write the POI filesystem back to the original file.</p></li>
*
* </ol>
*
* @author Rainer Klute <a
* href="mailto:klute@rainer-klute.de">klute@rainer-klute.de</a>
*/
public class ModifyDocumentSummaryInformation {
/**
* <p>Main method - see class description.</p>
*
* @param args The command-line parameters.
* @throws IOException
* @throws MarkUnsupportedException
* @throws NoPropertySetStreamException
* @throws UnexpectedPropertySetTypeException
* @throws WritingNotSupportedException
*/
public static void main(final String[] args) throws IOException,
NoPropertySetStreamException, MarkUnsupportedException,
UnexpectedPropertySetTypeException, WritingNotSupportedException
{
/* Read the name of the POI filesystem to modify from the command line.
* For brevity to boundary check is performed on the command-line
* arguments. */
File poiFilesystem = new File(args[0]);
/* Open the POI filesystem. */
InputStream is = new FileInputStream(poiFilesystem);
POIFSFileSystem poifs = new POIFSFileSystem(is);
is.close();
/* Read the summary information. */
DirectoryEntry dir = poifs.getRoot();
SummaryInformation si;
try
{
DocumentEntry siEntry = (DocumentEntry)
dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
DocumentInputStream dis = new DocumentInputStream(siEntry);
PropertySet ps = new PropertySet(dis);
dis.close();
si = new SummaryInformation(ps);
}
catch (FileNotFoundException ex)
{
/* There is no summary information yet. We have to create a new
* one. */
si = PropertySetFactory.newSummaryInformation();
}
/* Change the author to "Rainer Klute". Any former author value will
* be lost. If there has been no author yet, it will be created. */
si.setAuthor("Rainer Klute");
System.out.println("Author changed to " + si.getAuthor() + ".");
/* Handling the document summary information is analogous to handling
* the summary information. An additional feature, however, are the
* custom properties. */
/* Read the document summary information. */
DocumentSummaryInformation dsi;
try
{
DocumentEntry dsiEntry = (DocumentEntry)
dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
DocumentInputStream dis = new DocumentInputStream(dsiEntry);
PropertySet ps = new PropertySet(dis);
dis.close();
dsi = new DocumentSummaryInformation(ps);
}
catch (FileNotFoundException ex)
{
/* There is no document summary information yet. We have to create a
* new one. */
dsi = PropertySetFactory.newDocumentSummaryInformation();
}
/* Change the category to "POI example". Any former category value will
* be lost. If there has been no category yet, it will be created. */
dsi.setCategory("POI example");
System.out.println("Category changed to " + dsi.getCategory() + ".");
/* Read the custom properties. If there are no custom properties yet,
* the application has to create a new CustomProperties object. It will
* serve as a container for custom properties. */
CustomProperties customProperties = dsi.getCustomProperties();
if (customProperties == null)
customProperties = new CustomProperties();
/* Insert some custom properties into the container. */
customProperties.put("Key 1", "Value 1");
customProperties.put("Schl\u00fcssel 2", "Wert 2");
customProperties.put("Sample Number", new Integer(12345));
customProperties.put("Sample Boolean", Boolean.TRUE);
customProperties.put("Sample Date", new Date());
/* Read a custom property. */
Object value = customProperties.get("Sample Number");
/* Write the custom properties back to the document summary
* information. */
dsi.setCustomProperties(customProperties);
/* Write the summary information and the document summary information
* to the POI filesystem. */
si.write(dir, SummaryInformation.DEFAULT_STREAM_NAME);
dsi.write(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
/* Write the POI filesystem back to the original file. Please note that
* in production code you should never write directly to the origin
* file! In case of a writing error everything would be lost. */
OutputStream out = new FileOutputStream(poiFilesystem);
poifs.writeFilesystem(out);
out.close();
}
}

View File

@ -0,0 +1,137 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hpsf.examples;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.apache.poi.hpsf.NoPropertySetStreamException;
import org.apache.poi.hpsf.Property;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.Section;
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
import org.apache.poi.util.HexDump;
/**
* <p>Sample application showing how to read a document's custom property set.
* Call it with the document's file name as command-line parameter.</p>
*
* <p>Explanations can be found in the HPSF HOW-TO.</p>
*
* @author Rainer Klute <a
* href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
*/
public class ReadCustomPropertySets
{
/**
* <p>Runs the example program.</p>
*
* @param args Command-line arguments (unused).
* @throws IOException if any I/O exception occurs.
*/
public static void main(final String[] args)
throws IOException
{
final String filename = args[0];
POIFSReader r = new POIFSReader();
/* Register a listener for *all* documents. */
r.registerListener(new MyPOIFSReaderListener());
r.read(new FileInputStream(filename));
}
static class MyPOIFSReaderListener implements POIFSReaderListener
{
public void processPOIFSReaderEvent(final POIFSReaderEvent event)
{
PropertySet ps = null;
try
{
ps = PropertySetFactory.create(event.getStream());
}
catch (NoPropertySetStreamException ex)
{
out("No property set stream: \"" + event.getPath() +
event.getName() + "\"");
return;
}
catch (Exception ex)
{
throw new RuntimeException
("Property set stream \"" +
event.getPath() + event.getName() + "\": " + ex);
}
/* Print the name of the property set stream: */
out("Property set stream \"" + event.getPath() +
event.getName() + "\":");
/* Print the number of sections: */
final long sectionCount = ps.getSectionCount();
out(" No. of sections: " + sectionCount);
/* Print the list of sections: */
List sections = ps.getSections();
int nr = 0;
for (Iterator i = sections.iterator(); i.hasNext();)
{
/* Print a single section: */
Section sec = (Section) i.next();
out(" Section " + nr++ + ":");
String s = hex(sec.getFormatID().getBytes());
s = s.substring(0, s.length() - 1);
out(" Format ID: " + s);
/* Print the number of properties in this section. */
int propertyCount = sec.getPropertyCount();
out(" No. of properties: " + propertyCount);
/* Print the properties: */
Property[] properties = sec.getProperties();
for (int i2 = 0; i2 < properties.length; i2++)
{
/* Print a single property: */
Property p = properties[i2];
long id = p.getID();
long type = p.getType();
Object value = p.getValue();
out(" Property ID: " + id + ", type: " + type +
", value: " + value);
}
}
}
}
static void out(final String msg)
{
System.out.println(msg);
}
static String hex(final byte[] bytes)
{
return HexDump.dump(bytes, 0L, 0);
}
}

View File

@ -0,0 +1,82 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hpsf.examples;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
/**
* <p>Sample application showing how to read a OLE 2 document's
* title. Call it with the document's file name as command line
* parameter.</p>
*
* <p>Explanations can be found in the HPSF HOW-TO.</p>
*
* @author Rainer Klute <a
* href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
*/
public class ReadTitle
{
/**
* <p>Runs the example program.</p>
*
* @param args Command-line arguments. The first command-line argument must
* be the name of a POI filesystem to read.
* @throws IOException if any I/O exception occurs.
*/
public static void main(final String[] args) throws IOException
{
final String filename = args[0];
POIFSReader r = new POIFSReader();
r.registerListener(new MyPOIFSReaderListener(),
"\005SummaryInformation");
r.read(new FileInputStream(filename));
}
static class MyPOIFSReaderListener implements POIFSReaderListener
{
public void processPOIFSReaderEvent(final POIFSReaderEvent event)
{
SummaryInformation si = null;
try
{
si = (SummaryInformation)
PropertySetFactory.create(event.getStream());
}
catch (Exception ex)
{
throw new RuntimeException
("Property set stream \"" +
event.getPath() + event.getName() + "\": " + ex);
}
final String title = si.getTitle();
if (title != null)
System.out.println("Title: \"" + title + "\"");
else
System.out.println("Document has no title.");
}
}
}

View File

@ -0,0 +1,423 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hpsf.examples;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.hpsf.HPSFRuntimeException;
import org.apache.poi.hpsf.MarkUnsupportedException;
import org.apache.poi.hpsf.MutablePropertySet;
import org.apache.poi.hpsf.MutableSection;
import org.apache.poi.hpsf.NoPropertySetStreamException;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hpsf.Util;
import org.apache.poi.hpsf.Variant;
import org.apache.poi.hpsf.WritingNotSupportedException;
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.POIFSDocumentPath;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* <p>This class is a sample application which shows how to write or modify the
* author and title property of an OLE 2 document. This could be done in two
* different ways:</p>
*
* <ul>
*
* <li><p>The first approach is to open the OLE 2 file as a POI filesystem
* (see class {@link POIFSFileSystem}), read the summary information property
* set (see classes {@link SummaryInformation} and {@link PropertySet}), write
* the author and title properties into it and write the property set back into
* the POI filesystem.</p></li>
*
* <li><p>The second approach does not modify the original POI filesystem, but
* instead creates a new one. All documents from the original POIFS are copied
* to the destination POIFS, except for the summary information stream. The
* latter is modified by setting the author and title property before writing
* it to the destination POIFS. It there are several summary information streams
* in the original POIFS - e.g. in subordinate directories - they are modified
* just the same.</p></li>
*
* </ul>
*
* <p>This sample application takes the second approach. It expects the name of
* the existing POI filesystem's name as its first command-line parameter and
* the name of the output POIFS as the second command-line argument. The
* program then works as described above: It copies nearly all documents
* unmodified from the input POI filesystem to the output POI filesystem. If it
* encounters a summary information stream it reads its properties. Then it sets
* the "author" and "title" properties to new values and writes the modified
* summary information stream into the output file.</p>
*
* <p>Further explanations can be found in the HPSF HOW-TO.</p>
*
* @author Rainer Klute <a
* href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
*/
public class WriteAuthorAndTitle
{
/**
* <p>Runs the example program.</p>
*
* @param args Command-line arguments. The first command-line argument must
* be the name of a POI filesystem to read.
* @throws IOException if any I/O exception occurs.
*/
public static void main(final String[] args) throws IOException
{
/* Check whether we have exactly two command-line arguments. */
if (args.length != 2)
{
System.err.println("Usage: " + WriteAuthorAndTitle.class.getName() +
" originPOIFS destinationPOIFS");
System.exit(1);
}
/* Read the names of the origin and destination POI filesystems. */
final String srcName = args[0];
final String dstName = args[1];
/* Read the origin POIFS using the eventing API. The real work is done
* in the class ModifySICopyTheRest which is registered here as a
* POIFSReader. */
final POIFSReader r = new POIFSReader();
final ModifySICopyTheRest msrl = new ModifySICopyTheRest(dstName);
r.registerListener(msrl);
r.read(new FileInputStream(srcName));
/* Write the new POIFS to disk. */
msrl.close();
}
/**
* <p>This class does all the work. As its name implies it modifies a
* summary information property set and copies everything else unmodified
* to the destination POI filesystem. Since an instance of it is registered
* as a {@link POIFSReader} its method {@link
* #processPOIFSReaderEvent(POIFSReaderEvent)} is called for each document
* in the origin POIFS.</p>
*/
static class ModifySICopyTheRest implements POIFSReaderListener
{
String dstName;
OutputStream out;
POIFSFileSystem poiFs;
/**
* <p>The constructor of a {@link ModifySICopyTheRest} instance creates
* the target POIFS. It also stores the name of the file the POIFS will
* be written to once it is complete.</p>
*
* @param dstName The name of the disk file the destination POIFS is to
* be written to.
*/
public ModifySICopyTheRest(final String dstName)
{
this.dstName = dstName;
poiFs = new POIFSFileSystem();
}
/**
* <p>The method is called by POI's eventing API for each file in the
* origin POIFS.</p>
*/
public void processPOIFSReaderEvent(final POIFSReaderEvent event)
{
/* The following declarations are shortcuts for accessing the
* "event" object. */
final POIFSDocumentPath path = event.getPath();
final String name = event.getName();
final DocumentInputStream stream = event.getStream();
Throwable t = null;
try
{
/* Find out whether the current document is a property set
* stream or not. */
if (PropertySet.isPropertySetStream(stream))
{
/* Yes, the current document is a property set stream.
* Let's create a PropertySet instance from it. */
PropertySet ps = null;
try
{
ps = PropertySetFactory.create(stream);
}
catch (NoPropertySetStreamException ex)
{
/* This exception will not be thrown because we already
* checked above. */
}
/* Now we know that we really have a property set. The next
* step is to find out whether it is a summary information
* or not. */
if (ps.isSummaryInformation())
/* Yes, it is a summary information. We will modify it
* and write the result to the destination POIFS. */
editSI(poiFs, path, name, ps);
else
/* No, it is not a summary information. We don't care
* about its internals and copy it unmodified to the
* destination POIFS. */
copy(poiFs, path, name, ps);
}
else
/* No, the current document is not a property set stream. We
* copy it unmodified to the destination POIFS. */
copy(poiFs, event.getPath(), event.getName(), stream);
}
catch (MarkUnsupportedException ex)
{
t = ex;
}
catch (IOException ex)
{
t = ex;
}
catch (WritingNotSupportedException ex)
{
t = ex;
}
/* According to the definition of the processPOIFSReaderEvent method
* we cannot pass checked exceptions to the caller. The following
* lines check whether a checked exception occured and throws an
* unchecked exception. The message of that exception is that of
* the underlying checked exception. */
if (t != null)
{
throw new HPSFRuntimeException
("Could not read file \"" + path + "/" + name +
"\". Reason: " + Util.toString(t));
}
}
/**
* <p>Receives a summary information property set modifies (or creates)
* its "author" and "title" properties and writes the result under the
* same path and name as the origin to a destination POI filesystem.</p>
*
* @param poiFs The POI filesystem to write to.
* @param path The original (and destination) stream's path.
* @param name The original (and destination) stream's name.
* @param si The property set. It should be a summary information
* property set.
* @throws IOException
* @throws WritingNotSupportedException
*/
public void editSI(final POIFSFileSystem poiFs,
final POIFSDocumentPath path,
final String name,
final PropertySet si)
throws WritingNotSupportedException, IOException
{
/* Get the directory entry for the target stream. */
final DirectoryEntry de = getPath(poiFs, path);
/* Create a mutable property set as a copy of the original read-only
* property set. */
final MutablePropertySet mps = new MutablePropertySet(si);
/* Retrieve the section containing the properties to modify. A
* summary information property set contains exactly one section. */
final MutableSection s =
(MutableSection) mps.getSections().get(0);
/* Set the properties. */
s.setProperty(PropertyIDMap.PID_AUTHOR, Variant.VT_LPSTR,
"Rainer Klute");
s.setProperty(PropertyIDMap.PID_TITLE, Variant.VT_LPWSTR,
"Test");
/* Create an input stream containing the bytes the property set
* stream consists of. */
final InputStream pss = mps.toInputStream();
/* Write the property set stream to the POIFS. */
de.createDocument(name, pss);
}
/**
* <p>Writes a {@link PropertySet} to a POI filesystem. This method is
* simpler than {@link #editSI} because the origin property set has just
* to be copied.</p>
*
* @param poiFs The POI filesystem to write to.
* @param path The file's path in the POI filesystem.
* @param name The file's name in the POI filesystem.
* @param ps The property set to write.
* @throws WritingNotSupportedException
* @throws IOException
*/
public void copy(final POIFSFileSystem poiFs,
final POIFSDocumentPath path,
final String name,
final PropertySet ps)
throws WritingNotSupportedException, IOException
{
final DirectoryEntry de = getPath(poiFs, path);
final MutablePropertySet mps = new MutablePropertySet(ps);
de.createDocument(name, mps.toInputStream());
}
/**
* <p>Copies the bytes from a {@link DocumentInputStream} to a new
* stream in a POI filesystem.</p>
*
* @param poiFs The POI filesystem to write to.
* @param path The source document's path.
* @param name The source document's name.
* @param stream The stream containing the source document.
* @throws IOException
*/
public void copy(final POIFSFileSystem poiFs,
final POIFSDocumentPath path,
final String name,
final DocumentInputStream stream) throws IOException
{
final DirectoryEntry de = getPath(poiFs, path);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
int c;
while ((c = stream.read()) != -1)
out.write(c);
stream.close();
out.close();
final InputStream in =
new ByteArrayInputStream(out.toByteArray());
de.createDocument(name, in);
}
/**
* <p>Writes the POI file system to a disk file.</p>
*
* @throws FileNotFoundException
* @throws IOException
*/
public void close() throws FileNotFoundException, IOException
{
out = new FileOutputStream(dstName);
poiFs.writeFilesystem(out);
out.close();
}
/** Contains the directory paths that have already been created in the
* output POI filesystem and maps them to their corresponding
* {@link org.apache.poi.poifs.filesystem.DirectoryNode}s. */
private final Map paths = new HashMap();
/**
* <p>Ensures that the directory hierarchy for a document in a POI
* fileystem is in place. When a document is to be created somewhere in
* a POI filesystem its directory must be created first. This method
* creates all directories between the POI filesystem root and the
* directory the document should belong to which do not yet exist.</p>
*
* <p>Unfortunately POI does not offer a simple method to interrogate
* the POIFS whether a certain child node (file or directory) exists in
* a directory. However, since we always start with an empty POIFS which
* contains the root directory only and since each directory in the
* POIFS is created by this method we can maintain the POIFS's directory
* hierarchy ourselves: The {@link DirectoryEntry} of each directory
* created is stored in a {@link Map}. The directories' path names map
* to the corresponding {@link DirectoryEntry} instances.</p>
*
* @param poiFs The POI filesystem the directory hierarchy is created
* in, if needed.
* @param path The document's path. This method creates those directory
* components of this hierarchy which do not yet exist.
* @return The directory entry of the document path's parent. The caller
* should use this {@link DirectoryEntry} to create documents in it.
*/
public DirectoryEntry getPath(final POIFSFileSystem poiFs,
final POIFSDocumentPath path)
{
try
{
/* Check whether this directory has already been created. */
final String s = path.toString();
DirectoryEntry de = (DirectoryEntry) paths.get(s);
if (de != null)
/* Yes: return the corresponding DirectoryEntry. */
return de;
/* No: We have to create the directory - or return the root's
* DirectoryEntry. */
int l = path.length();
if (l == 0)
/* Get the root directory. It does not have to be created
* since it always exists in a POIFS. */
de = poiFs.getRoot();
else
{
/* Create a subordinate directory. The first step is to
* ensure that the parent directory exists: */
de = getPath(poiFs, path.getParent());
/* Now create the target directory: */
de = de.createDirectory(path.getComponent
(path.length() - 1));
}
paths.put(s, de);
return de;
}
catch (IOException ex)
{
/* This exception will be thrown if the directory already
* exists. However, since we have full control about directory
* creation we can ensure that this will never happen. */
ex.printStackTrace(System.err);
throw new RuntimeException(ex.toString());
/* FIXME (2): Replace the previous line by the following once we
* no longer need JDK 1.3 compatibility. */
// throw new RuntimeException(ex);
}
}
}
}

View File

@ -0,0 +1,106 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hpsf.examples;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hpsf.MutableProperty;
import org.apache.poi.hpsf.MutablePropertySet;
import org.apache.poi.hpsf.MutableSection;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hpsf.Variant;
import org.apache.poi.hpsf.WritingNotSupportedException;
import org.apache.poi.hpsf.wellknown.PropertyIDMap;
import org.apache.poi.hpsf.wellknown.SectionIDMap;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* <p>This class is a simple sample application showing how to create a property
* set and write it to disk.</p>
*
* @author Rainer Klute <a
* href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
*/
public class WriteTitle
{
/**
* <p>Runs the example program.</p>
*
* @param args Command-line arguments. The first and only command-line
* argument is the name of the POI file system to create.
* @throws IOException if any I/O exception occurs.
* @throws WritingNotSupportedException if HPSF does not (yet) support
* writing a certain property type.
*/
public static void main(final String[] args)
throws WritingNotSupportedException, IOException
{
/* Check whether we have exactly one command-line argument. */
if (args.length != 1)
{
System.err.println("Usage: " + WriteTitle.class.getName() +
"destinationPOIFS");
System.exit(1);
}
final String fileName = args[0];
/* Create a mutable property set. Initially it contains a single section
* with no properties. */
final MutablePropertySet mps = new MutablePropertySet();
/* Retrieve the section the property set already contains. */
final MutableSection ms = (MutableSection) mps.getSections().get(0);
/* Turn the property set into a summary information property. This is
* done by setting the format ID of its first section to
* SectionIDMap.SUMMARY_INFORMATION_ID. */
ms.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID);
/* Create an empty property. */
final MutableProperty p = new MutableProperty();
/* Fill the property with appropriate settings so that it specifies the
* document's title. */
p.setID(PropertyIDMap.PID_TITLE);
p.setType(Variant.VT_LPWSTR);
p.setValue("Sample title");
/* Place the property into the section. */
ms.setProperty(p);
/* Create the POI file system the property set is to be written to. */
final POIFSFileSystem poiFs = new POIFSFileSystem();
/* For writing the property set into a POI file system it has to be
* handed over to the POIFS.createDocument() method as an input stream
* which produces the bytes making out the property set stream. */
final InputStream is = mps.toInputStream();
/* Create the summary information property set in the POI file
* system. It is given the default name most (if not all) summary
* information property sets have. */
poiFs.createDocument(is, SummaryInformation.DEFAULT_STREAM_NAME);
/* Write the whole POI file system to a disk file. */
poiFs.writeFilesystem(new FileOutputStream(fileName));
}
}

View File

@ -0,0 +1,515 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.examples;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.hslf.model.*;
import org.apache.poi.hslf.record.TextHeaderAtom;
import java.io.IOException;
import java.io.FileOutputStream;
import java.awt.*;
/**
* Presentation for Fast Feather Track on ApacheconEU 2008
*
* @author Yegor Kozlov
*/
public final class ApacheconEU08 {
public static void main(String[] args) throws IOException {
SlideShow ppt = new SlideShow();
ppt.setPageSize(new Dimension(720, 540));
slide1(ppt);
slide2(ppt);
slide3(ppt);
slide4(ppt);
slide5(ppt);
slide6(ppt);
slide7(ppt);
slide8(ppt);
slide9(ppt);
slide10(ppt);
slide11(ppt);
slide12(ppt);
FileOutputStream out = new FileOutputStream("apachecon_eu_08.ppt");
ppt.write(out);
out.close();
}
public static void slide1(SlideShow ppt) throws IOException {
Slide slide = ppt.createSlide();
TextBox box1 = new TextBox();
TextRun tr1 = box1.getTextRun();
tr1.setRunType(TextHeaderAtom.CENTER_TITLE_TYPE);
tr1.setText("POI-HSLF");
box1.setAnchor(new Rectangle(54, 78, 612, 115));
slide.addShape(box1);
TextBox box2 = new TextBox();
TextRun tr2 = box2.getTextRun();
tr2.setRunType(TextHeaderAtom.CENTRE_BODY_TYPE);
tr2.setText("Java API To Access Microsoft PowerPoint Format Files");
box2.setAnchor(new Rectangle(108, 204, 504, 138));
slide.addShape(box2);
TextBox box3 = new TextBox();
TextRun tr3 = box3.getTextRun();
tr3.getRichTextRuns()[0].setFontSize(32);
box3.setHorizontalAlignment(TextBox.AlignCenter);
tr3.setText(
"Yegor Kozlov\r" +
"yegor - apache - org");
box3.setAnchor(new Rectangle(206, 348, 310, 84));
slide.addShape(box3);
}
public static void slide2(SlideShow ppt) throws IOException {
Slide slide = ppt.createSlide();
TextBox box1 = new TextBox();
TextRun tr1 = box1.getTextRun();
tr1.setRunType(TextHeaderAtom.TITLE_TYPE);
tr1.setText("What is HSLF?");
box1.setAnchor(new Rectangle(36, 21, 648, 90));
slide.addShape(box1);
TextBox box2 = new TextBox();
TextRun tr2 = box2.getTextRun();
tr2.setRunType(TextHeaderAtom.BODY_TYPE);
tr2.setText("HorribleSLideshowFormat is the POI Project's pure Java implementation " +
"of the Powerpoint binary file format. \r" +
"POI sub-project since 2005\r" +
"Started by Nick Birch, Yegor Kozlov joined soon after");
box2.setAnchor(new Rectangle(36, 126, 648, 356));
slide.addShape(box2);
}
public static void slide3(SlideShow ppt) throws IOException {
Slide slide = ppt.createSlide();
TextBox box1 = new TextBox();
TextRun tr1 = box1.getTextRun();
tr1.setRunType(TextHeaderAtom.TITLE_TYPE);
tr1.setText("HSLF in a Nutshell");
box1.setAnchor(new Rectangle(36, 15, 648, 65));
slide.addShape(box1);
TextBox box2 = new TextBox();
TextRun tr2 = box2.getTextRun();
tr2.setRunType(TextHeaderAtom.BODY_TYPE);
tr2.setText(
"HSLF provides a way to read, create and modify MS PowerPoint presentations\r" +
"Pure Java API - you don't need PowerPoint to read and write *.ppt files\r" +
"Comprehensive support of PowerPoint objects");
tr2.getRichTextRuns()[0].setFontSize(28);
box2.setAnchor(new Rectangle(36, 80, 648, 200));
slide.addShape(box2);
TextBox box3 = new TextBox();
TextRun tr3 = box3.getTextRun();
tr3.setRunType(TextHeaderAtom.BODY_TYPE);
tr3.setText(
"Rich text\r" +
"Tables\r" +
"Shapes\r" +
"Pictures\r" +
"Master slides");
tr3.getRichTextRuns()[0].setFontSize(24);
tr3.getRichTextRuns()[0].setIndentLevel(1);
box3.setAnchor(new Rectangle(36, 265, 648, 150));
slide.addShape(box3);
TextBox box4 = new TextBox();
TextRun tr4 = box4.getTextRun();
tr4.setRunType(TextHeaderAtom.BODY_TYPE);
tr4.setText("Access to low level data structures");
box4.setAnchor(new Rectangle(36, 430, 648, 50));
slide.addShape(box4);
}
public static void slide4(SlideShow ppt) throws IOException {
Slide slide = ppt.createSlide();
String[][] txt1 = {
{"Note"},
{"This presentation was created programmatically using POI HSLF"}
};
Table table1 = new Table(2, 1);
for (int i = 0; i < txt1.length; i++) {
for (int j = 0; j < txt1[i].length; j++) {
TableCell cell = table1.getCell(i, j);
cell.setText(txt1[i][j]);
cell.getTextRun().getRichTextRuns()[0].setFontSize(10);
RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("Arial");
rt.setBold(true);
if(i == 0){
rt.setFontSize(32);
rt.setFontColor(Color.white);
cell.getFill().setForegroundColor(new Color(0, 153, 204));
} else {
rt.setFontSize(28);
cell.getFill().setForegroundColor(new Color(235, 239, 241));
}
cell.setVerticalAlignment(TextBox.AnchorMiddle);
}
}
Line border1 = table1.createBorder();
border1.setLineColor(Color.black);
border1.setLineWidth(1.0);
table1.setAllBorders(border1);
Line border2 = table1.createBorder();
border2.setLineColor(Color.black);
border2.setLineWidth(2.0);
table1.setOutsideBorders(border2);
table1.setColumnWidth(0, 510);
table1.setRowHeight(0, 60);
table1.setRowHeight(1, 100);
slide.addShape(table1);
table1.moveTo(100, 100);
TextBox box1 = new TextBox();
box1.setHorizontalAlignment(TextBox.AlignCenter);
TextRun tr1 = box1.getTextRun();
tr1.setText("The source code is available at\r" +
"http://people.apache.org/~yegor/apachecon_eu08/");
RichTextRun rt = tr1.getRichTextRuns()[0];
rt.setFontSize(24);
box1.setAnchor(new Rectangle(80, 356, 553, 65));
slide.addShape(box1);
}
public static void slide5(SlideShow ppt) throws IOException {
Slide slide = ppt.createSlide();
TextBox box1 = new TextBox();
TextRun tr1 = box1.getTextRun();
tr1.setRunType(TextHeaderAtom.TITLE_TYPE);
tr1.setText("HSLF in Action - 1\rData Extraction");
box1.setAnchor(new Rectangle(36, 21, 648, 100));
slide.addShape(box1);
TextBox box2 = new TextBox();
TextRun tr2 = box2.getTextRun();
tr2.setRunType(TextHeaderAtom.BODY_TYPE);
tr2.setText(
"Text from slides and notes\r" +
"Images\r" +
"Shapes and their properties (type, position in the slide, color, font, etc.)");
box2.setAnchor(new Rectangle(36, 150, 648, 300));
slide.addShape(box2);
}
public static void slide6(SlideShow ppt) throws IOException {
Slide slide = ppt.createSlide();
TextBox box1 = new TextBox();
TextRun tr1 = box1.getTextRun();
tr1.setRunType(TextHeaderAtom.TITLE_TYPE);
tr1.setText("HSLF in Action - 2");
box1.setAnchor(new Rectangle(36, 20, 648, 90));
slide.addShape(box1);
TextBox box2 = new TextBox();
TextRun tr2 = box2.getTextRun();
tr2.getRichTextRuns()[0].setFontSize(18);
tr2.setText("Creating a simple presentation from scratch");
box2.setAnchor(new Rectangle(170, 100, 364, 30));
slide.addShape(box2);
TextBox box3 = new TextBox();
TextRun tr3 = box3.getTextRun();
RichTextRun rt3 = tr3.getRichTextRuns()[0];
rt3.setFontName("Courier New");
rt3.setFontSize(8);
tr3.setText(
" SlideShow ppt = new SlideShow();\r" +
" Slide slide = ppt.createSlide();\r" +
"\r" +
" TextBox box2 = new TextBox();\r" +
" box2.setHorizontalAlignment(TextBox.AlignCenter);\r" +
" box2.setVerticalAlignment(TextBox.AnchorMiddle);\r" +
" box2.getTextRun().setText(\"Java Code\");\r" +
" box2.getFill().setForegroundColor(new Color(187, 224, 227));\r" +
" box2.setLineColor(Color.black);\r" +
" box2.setLineWidth(0.75);\r" +
" box2.setAnchor(new Rectangle(66, 243, 170, 170));\r" +
" slide.addShape(box2);\r" +
"\r" +
" TextBox box3 = new TextBox();\r" +
" box3.setHorizontalAlignment(TextBox.AlignCenter);\r" +
" box3.setVerticalAlignment(TextBox.AnchorMiddle);\r" +
" box3.getTextRun().setText(\"*.ppt file\");\r" +
" box3.setLineWidth(0.75);\r" +
" box3.setLineColor(Color.black);\r" +
" box3.getFill().setForegroundColor(new Color(187, 224, 227));\r" +
" box3.setAnchor(new Rectangle(473, 243, 170, 170));\r" +
" slide.addShape(box3);\r" +
"\r" +
" AutoShape box4 = new AutoShape(ShapeTypes.Arrow);\r" +
" box4.getFill().setForegroundColor(new Color(187, 224, 227));\r" +
" box4.setLineWidth(0.75);\r" +
" box4.setLineColor(Color.black);\r" +
" box4.setAnchor(new Rectangle(253, 288, 198, 85));\r" +
" slide.addShape(box4);\r" +
"\r" +
" FileOutputStream out = new FileOutputStream(\"hslf-demo.ppt\");\r" +
" ppt.write(out);\r" +
" out.close();");
box3.setAnchor(new Rectangle(30, 150, 618, 411));
slide.addShape(box3);
}
public static void slide7(SlideShow ppt) throws IOException {
Slide slide = ppt.createSlide();
TextBox box2 = new TextBox();
box2.setHorizontalAlignment(TextBox.AlignCenter);
box2.setVerticalAlignment(TextBox.AnchorMiddle);
box2.getTextRun().setText("Java Code");
box2.getFill().setForegroundColor(new Color(187, 224, 227));
box2.setLineColor(Color.black);
box2.setLineWidth(0.75);
box2.setAnchor(new Rectangle(66, 243, 170, 170));
slide.addShape(box2);
TextBox box3 = new TextBox();
box3.setHorizontalAlignment(TextBox.AlignCenter);
box3.setVerticalAlignment(TextBox.AnchorMiddle);
box3.getTextRun().setText("*.ppt file");
box3.setLineWidth(0.75);
box3.setLineColor(Color.black);
box3.getFill().setForegroundColor(new Color(187, 224, 227));
box3.setAnchor(new Rectangle(473, 243, 170, 170));
slide.addShape(box3);
AutoShape box4 = new AutoShape(ShapeTypes.Arrow);
box4.getFill().setForegroundColor(new Color(187, 224, 227));
box4.setLineWidth(0.75);
box4.setLineColor(Color.black);
box4.setAnchor(new Rectangle(253, 288, 198, 85));
slide.addShape(box4);
}
public static void slide8(SlideShow ppt) throws IOException {
Slide slide = ppt.createSlide();
TextBox box1 = new TextBox();
TextRun tr1 = box1.getTextRun();
tr1.setRunType(TextHeaderAtom.TITLE_TYPE);
tr1.setText("Wait, there is more!");
box1.setAnchor(new Rectangle(36, 21, 648, 90));
slide.addShape(box1);
TextBox box2 = new TextBox();
TextRun tr2 = box2.getTextRun();
tr2.setRunType(TextHeaderAtom.BODY_TYPE);
tr2.setText(
"Rich text\r" +
"Tables\r" +
"Pictures (JPEG, PNG, BMP, WMF, PICT)\r" +
"Comprehensive formatting features");
box2.setAnchor(new Rectangle(36, 126, 648, 356));
slide.addShape(box2);
}
public static void slide9(SlideShow ppt) throws IOException {
Slide slide = ppt.createSlide();
TextBox box1 = new TextBox();
TextRun tr1 = box1.getTextRun();
tr1.setRunType(TextHeaderAtom.TITLE_TYPE);
tr1.setText("HSLF in Action - 3");
box1.setAnchor(new Rectangle(36, 20, 648, 50));
slide.addShape(box1);
TextBox box2 = new TextBox();
TextRun tr2 = box2.getTextRun();
tr2.getRichTextRuns()[0].setFontSize(18);
tr2.setText("PPGraphics2D: PowerPoint Graphics2D driver");
box2.setAnchor(new Rectangle(178, 70, 387, 30));
slide.addShape(box2);
TextBox box3 = new TextBox();
TextRun tr3 = box3.getTextRun();
RichTextRun rt3 = tr3.getRichTextRuns()[0];
rt3.setFontName("Courier New");
rt3.setFontSize(8);
tr3.setText(
" //bar chart data. The first value is the bar color, the second is the width\r" +
" Object[] def = new Object[]{\r" +
" Color.yellow, new Integer(100),\r" +
" Color.green, new Integer(150),\r" +
" Color.gray, new Integer(75),\r" +
" Color.red, new Integer(200),\r" +
" };\r" +
"\r" +
" SlideShow ppt = new SlideShow();\r" +
" Slide slide = ppt.createSlide();\r" +
"\r" +
" ShapeGroup group = new ShapeGroup();\r" +
" //define position of the drawing in the slide\r" +
" Rectangle bounds = new java.awt.Rectangle(200, 100, 350, 300);\r" +
" group.setAnchor(bounds);\r" +
" slide.addShape(group);\r" +
" Graphics2D graphics = new PPGraphics2D(group);\r" +
"\r" +
" //draw a simple bar graph\r" +
" int x = bounds.x + 50, y = bounds.y + 50;\r" +
" graphics.setFont(new Font(\"Arial\", Font.BOLD, 10));\r" +
" for (int i = 0, idx = 1; i < def.length; i+=2, idx++) {\r" +
" graphics.setColor(Color.black);\r" +
" int width = ((Integer)def[i+1]).intValue();\r" +
" graphics.drawString(\"Q\" + idx, x-20, y+20);\r" +
" graphics.drawString(width + \"%\", x + width + 10, y + 20);\r" +
" graphics.setColor((Color)def[i]);\r" +
" graphics.fill(new Rectangle(x, y, width, 30));\r" +
" y += 40;\r" +
" }\r" +
" graphics.setColor(Color.black);\r" +
" graphics.setFont(new Font(\"Arial\", Font.BOLD, 14));\r" +
" graphics.draw(bounds);\r" +
" graphics.drawString(\"Performance\", x + 70, y + 40);\r" +
"\r" +
" FileOutputStream out = new FileOutputStream(\"hslf-demo.ppt\");\r" +
" ppt.write(out);\r" +
" out.close();");
box3.setAnchor(new Rectangle(96, 110, 499, 378));
slide.addShape(box3);
}
public static void slide10(SlideShow ppt) throws IOException {
//bar chart data. The first value is the bar color, the second is the width
Object[] def = new Object[]{
Color.yellow, new Integer(100),
Color.green, new Integer(150),
Color.gray, new Integer(75),
Color.red, new Integer(200),
};
Slide slide = ppt.createSlide();
ShapeGroup group = new ShapeGroup();
//define position of the drawing in the slide
Rectangle bounds = new java.awt.Rectangle(200, 100, 350, 300);
group.setAnchor(bounds);
slide.addShape(group);
Graphics2D graphics = new PPGraphics2D(group);
//draw a simple bar graph
int x = bounds.x + 50, y = bounds.y + 50;
graphics.setFont(new Font("Arial", Font.BOLD, 10));
for (int i = 0, idx = 1; i < def.length; i+=2, idx++) {
graphics.setColor(Color.black);
int width = ((Integer)def[i+1]).intValue();
graphics.drawString("Q" + idx, x-20, y+20);
graphics.drawString(width + "%", x + width + 10, y + 20);
graphics.setColor((Color)def[i]);
graphics.fill(new Rectangle(x, y, width, 30));
y += 40;
}
graphics.setColor(Color.black);
graphics.setFont(new Font("Arial", Font.BOLD, 14));
graphics.draw(bounds);
graphics.drawString("Performance", x + 70, y + 40);
}
public static void slide11(SlideShow ppt) throws IOException {
Slide slide = ppt.createSlide();
TextBox box1 = new TextBox();
TextRun tr1 = box1.getTextRun();
tr1.setRunType(TextHeaderAtom.TITLE_TYPE);
tr1.setText("HSLF Development Plans");
box1.setAnchor(new Rectangle(36, 21, 648, 90));
slide.addShape(box1);
TextBox box2 = new TextBox();
TextRun tr2 = box2.getTextRun();
tr2.setRunType(TextHeaderAtom.BODY_TYPE);
tr2.getRichTextRuns()[0].setFontSize(32);
tr2.setText(
"Support for more PowerPoint functionality\r" +
"Rendering slides into java.awt.Graphics2D");
box2.setAnchor(new Rectangle(36, 126, 648, 100));
slide.addShape(box2);
TextBox box3 = new TextBox();
TextRun tr3 = box3.getTextRun();
tr3.setRunType(TextHeaderAtom.BODY_TYPE);
tr3.getRichTextRuns()[0].setIndentLevel(1);
tr3.setText(
"A way to export slides into images or other formats");
box3.setAnchor(new Rectangle(36, 220, 648, 70));
slide.addShape(box3);
TextBox box4 = new TextBox();
TextRun tr4 = box4.getTextRun();
tr4.setRunType(TextHeaderAtom.BODY_TYPE);
tr4.getRichTextRuns()[0].setFontSize(32);
tr4.setText(
"Integration with Apache FOP - Formatting Objects Processor");
box4.setAnchor(new Rectangle(36, 290, 648, 90));
slide.addShape(box4);
TextBox box5 = new TextBox();
TextRun tr5 = box5.getTextRun();
tr5.setRunType(TextHeaderAtom.BODY_TYPE);
tr5.getRichTextRuns()[0].setIndentLevel(1);
tr5.setText(
"Transformation of XSL-FO into PPT\r" +
"PPT2PDF transcoder");
box5.setAnchor(new Rectangle(36, 380, 648, 100));
slide.addShape(box5);
}
public static void slide12(SlideShow ppt) throws IOException {
Slide slide = ppt.createSlide();
TextBox box1 = new TextBox();
TextRun tr1 = box1.getTextRun();
tr1.setRunType(TextHeaderAtom.CENTER_TITLE_TYPE);
tr1.setText("Questions?");
box1.setAnchor(new Rectangle(54, 167, 612, 115));
slide.addShape(box1);
TextBox box2 = new TextBox();
TextRun tr2 = box2.getTextRun();
tr2.setRunType(TextHeaderAtom.CENTRE_BODY_TYPE);
tr2.setText(
"http://poi.apache.org/hslf/\r" +
"http://people.apache.org/~yegor");
box2.setAnchor(new Rectangle(108, 306, 504, 138));
slide.addShape(box2);
}
}

View File

@ -0,0 +1,62 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.examples;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextBox;
import java.io.FileOutputStream;
/**
* How to create a single-level bulleted list
* and change some of the bullet attributes
*
* @author Yegor Kozlov
*/
public final class BulletsDemo {
public static void main(String[] args) throws Exception {
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
TextBox shape = new TextBox();
RichTextRun rt = shape.getTextRun().getRichTextRuns()[0];
shape.setText(
"January\r" +
"February\r" +
"March\r" +
"April");
rt.setFontSize(42);
rt.setBullet(true);
rt.setBulletOffset(0); //bullet offset
rt.setTextOffset(50); //text offset (should be greater than bullet offset)
rt.setBulletChar('\u263A'); //bullet character
slide.addShape(shape);
shape.setAnchor(new java.awt.Rectangle(50, 50, 500, 300)); //position of the text box in the slide
slide.addShape(shape);
FileOutputStream out = new FileOutputStream("bullets.ppt");
ppt.write(out);
out.close();
}
}

View File

@ -0,0 +1,75 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.examples;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.model.*;
import java.io.FileOutputStream;
import java.awt.*;
/**
* Demonstrates how to create hyperlinks in PowerPoint presentations
*
* @author Yegor Kozlov
*/
public final class CreateHyperlink {
public static void main(String[] args) throws Exception {
SlideShow ppt = new SlideShow();
Slide slideA = ppt.createSlide();
Slide slideB = ppt.createSlide();
Slide slideC = ppt.createSlide();
// link to a URL
TextBox textBox1 = new TextBox();
textBox1.setText("Apache POI");
textBox1.setAnchor(new Rectangle(100, 100, 200, 50));
String text = textBox1.getText();
Hyperlink link = new Hyperlink();
link.setAddress("http://www.apache.org");
link.setTitle(textBox1.getText());
int linkId = ppt.addHyperlink(link);
// apply link to the text
textBox1.setHyperlink(linkId, 0, text.length());
slideA.addShape(textBox1);
// link to another slide
TextBox textBox2 = new TextBox();
textBox2.setText("Go to slide #3");
textBox2.setAnchor(new Rectangle(100, 300, 200, 50));
Hyperlink link2 = new Hyperlink();
link2.setAddress(slideC);
ppt.addHyperlink(link2);
// apply link to the whole shape
textBox2.setHyperlink(link2);
slideA.addShape(textBox2);
FileOutputStream out = new FileOutputStream("hyperlink.ppt");
ppt.write(out);
out.close();
}
}

View File

@ -0,0 +1,148 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.examples;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.hslf.model.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Paragraph;
import java.io.*;
/**
* Demonstrates how you can extract misc embedded data from a ppt file
*
* @author Yegor Kozlov
*/
public final class DataExtraction {
public static void main(String args[]) throws Exception {
if (args.length == 0) {
usage();
return;
}
FileInputStream is = new FileInputStream(args[0]);
SlideShow ppt = new SlideShow(is);
is.close();
//extract all sound files embedded in this presentation
SoundData[] sound = ppt.getSoundData();
for (int i = 0; i < sound.length; i++) {
String type = sound[i].getSoundType(); //*.wav
String name = sound[i].getSoundName(); //typically file name
byte[] data = sound[i].getData(); //raw bytes
//save the sound on disk
FileOutputStream out = new FileOutputStream(name + type);
out.write(data);
out.close();
}
//extract embedded OLE documents
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
Shape[] shape = slide[i].getShapes();
for (int j = 0; j < shape.length; j++) {
if (shape[j] instanceof OLEShape) {
OLEShape ole = (OLEShape) shape[j];
ObjectData data = ole.getObjectData();
String name = ole.getInstanceName();
if ("Worksheet".equals(name)) {
//read xls
HSSFWorkbook wb = new HSSFWorkbook(data.getData());
} else if ("Document".equals(name)) {
HWPFDocument doc = new HWPFDocument(data.getData());
//read the word document
Range r = doc.getRange();
for(int k = 0; k < r.numParagraphs(); k++) {
Paragraph p = r.getParagraph(k);
System.out.println(p.text());
}
//save on disk
FileOutputStream out = new FileOutputStream(name + "-("+(j)+").doc");
doc.write(out);
out.close();
} else {
FileOutputStream out = new FileOutputStream(ole.getProgID() + "-"+(j+1)+".dat");
InputStream dis = data.getData();
byte[] chunk = new byte[2048];
int count;
while ((count = dis.read(chunk)) >= 0) {
out.write(chunk,0,count);
}
is.close();
out.close();
}
}
}
}
//Pictures
for (int i = 0; i < slide.length; i++) {
Shape[] shape = slide[i].getShapes();
for (int j = 0; j < shape.length; j++) {
if (shape[j] instanceof Picture) {
Picture p = (Picture) shape[j];
PictureData data = p.getPictureData();
String name = p.getPictureName();
int type = data.getType();
String ext;
switch (type) {
case Picture.JPEG:
ext = ".jpg";
break;
case Picture.PNG:
ext = ".png";
break;
case Picture.WMF:
ext = ".wmf";
break;
case Picture.EMF:
ext = ".emf";
break;
case Picture.PICT:
ext = ".pict";
break;
case Picture.DIB:
ext = ".dib";
break;
default:
continue;
}
FileOutputStream out = new FileOutputStream("pict-" + j + ext);
out.write(data.getData());
out.close();
}
}
}
}
private static void usage(){
System.out.println("Usage: DataExtraction ppt");
}
}

View File

@ -0,0 +1,79 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.examples;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.model.*;
import java.awt.*;
import java.io.FileOutputStream;
/**
* Demonstrates how to draw into a slide using the HSLF Graphics2D driver.
*
* @author Yegor Kozlov
*/
public final class Graphics2DDemo {
/**
* A simple bar chart demo
*/
public static void main(String[] args) throws Exception {
SlideShow ppt = new SlideShow();
//bar chart data. The first value is the bar color, the second is the width
Object[] def = new Object[]{
Color.yellow, new Integer(40),
Color.green, new Integer(60),
Color.gray, new Integer(30),
Color.red, new Integer(80),
};
Slide slide = ppt.createSlide();
ShapeGroup group = new ShapeGroup();
//define position of the drawing in the slide
Rectangle bounds = new java.awt.Rectangle(200, 100, 350, 300);
group.setAnchor(bounds);
group.setCoordinates(new java.awt.Rectangle(0, 0, 100, 100));
slide.addShape(group);
Graphics2D graphics = new PPGraphics2D(group);
//draw a simple bar graph
int x = 10, y = 10;
graphics.setFont(new Font("Arial", Font.BOLD, 10));
for (int i = 0, idx = 1; i < def.length; i+=2, idx++) {
graphics.setColor(Color.black);
int width = ((Integer)def[i+1]).intValue();
graphics.drawString("Q" + idx, x-5, y+10);
graphics.drawString(width + "%", x + width+3, y + 10);
graphics.setColor((Color)def[i]);
graphics.fill(new Rectangle(x, y, width, 10));
y += 15;
}
graphics.setColor(Color.black);
graphics.setFont(new Font("Arial", Font.BOLD, 14));
graphics.draw(group.getCoordinates());
graphics.drawString("Performance", x + 30, y + 10);
FileOutputStream out = new FileOutputStream("hslf-graphics.ppt");
ppt.write(out);
out.close();
}
}

View File

@ -0,0 +1,51 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.examples;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.model.HeadersFooters;
import org.apache.poi.hslf.model.Slide;
import java.io.FileOutputStream;
/**
* Demonstrates how to set headers / footers
*
* @author Yegor Kozlov
*/
public class HeadersFootersDemo {
public static void main(String[] args) throws Exception {
SlideShow ppt = new SlideShow();
HeadersFooters slideHeaders = ppt.getSlideHeadersFooters();
slideHeaders.setFootersText("Created by POI-HSLF");
slideHeaders.setSlideNumberVisible(true);
slideHeaders.setDateTimeText("custom date time");
HeadersFooters notesHeaders = ppt.getNotesHeadersFooters();
notesHeaders.setFootersText("My notes footers");
notesHeaders.setHeaderText("My notes header");
Slide slide = ppt.createSlide();
FileOutputStream out = new FileOutputStream("headers_footers.ppt");
ppt.write(out);
out.close();
}
}

View File

@ -0,0 +1,81 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.examples;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.model.Hyperlink;
import org.apache.poi.hslf.model.Shape;
import java.io.FileInputStream;
/**
* Demonstrates how to read hyperlinks from a presentation
*
* @author Yegor Kozlov
*/
public final class Hyperlinks {
public static void main(String[] args) throws Exception {
for (int i = 0; i < args.length; i++) {
FileInputStream is = new FileInputStream(args[i]);
SlideShow ppt = new SlideShow(is);
is.close();
Slide[] slide = ppt.getSlides();
for (int j = 0; j < slide.length; j++) {
System.out.println("slide " + slide[j].getSlideNumber());
//read hyperlinks from the slide's text runs
System.out.println("reading hyperlinks from the text runs");
TextRun[] txt = slide[j].getTextRuns();
for (int k = 0; k < txt.length; k++) {
String text = txt[k].getText();
Hyperlink[] links = txt[k].getHyperlinks();
if(links != null) for (int l = 0; l < links.length; l++) {
Hyperlink link = links[l];
String title = link.getTitle();
String address = link.getAddress();
System.out.println(" " + title);
System.out.println(" " + address);
String substring = text.substring(link.getStartIndex(), link.getEndIndex()-1);//in ppt end index is inclusive
System.out.println(" " + substring);
}
}
//in PowerPoint you can assign a hyperlink to a shape without text,
//for example to a Line object. The code below demonstrates how to
//read such hyperlinks
System.out.println(" reading hyperlinks from the slide's shapes");
Shape[] sh = slide[j].getShapes();
for (int k = 0; k < sh.length; k++) {
Hyperlink link = sh[k].getHyperlink();
if(link != null) {
String title = link.getTitle();
String address = link.getAddress();
System.out.println(" " + title);
System.out.println(" " + address);
}
}
}
}
}
}

View File

@ -0,0 +1,103 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.examples;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.hslf.model.*;
import javax.imageio.ImageIO;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.geom.Rectangle2D;
/**
* Demonstrates how you can use HSLF to convert each slide into a PNG image
*
* @author Yegor Kozlov
*/
public final class PPT2PNG {
public static void main(String args[]) throws Exception {
if (args.length == 0) {
usage();
return;
}
int slidenum = -1;
float scale = 1;
String file = null;
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("-")) {
if ("-scale".equals(args[i])){
scale = Float.parseFloat(args[++i]);
} else if ("-slide".equals(args[i])) {
slidenum = Integer.parseInt(args[++i]);
}
} else {
file = args[i];
}
}
if(file == null){
usage();
return;
}
FileInputStream is = new FileInputStream(file);
SlideShow ppt = new SlideShow(is);
is.close();
Dimension pgsize = ppt.getPageSize();
int width = (int)(pgsize.width*scale);
int height = (int)(pgsize.height*scale);
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
if (slidenum != -1 && slidenum != (i+1)) continue;
String title = slide[i].getTitle();
System.out.println("Rendering slide "+slide[i].getSlideNumber() + (title == null ? "" : ": " + title));
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, width, height));
graphics.scale((double)width/pgsize.width, (double)height/pgsize.height);
slide[i].draw(graphics);
String fname = file.replaceAll("\\.ppt", "-" + (i+1) + ".png");
FileOutputStream out = new FileOutputStream(fname);
ImageIO.write(img, "png", out);
out.close();
}
}
private static void usage(){
System.out.println("Usage: PPT2PNG [-scale <scale> -slide <num>] ppt");
}
}

View File

@ -0,0 +1,80 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.examples;
import org.apache.poi.ddf.*;
import org.apache.poi.hslf.model.*;
import org.apache.poi.hslf.record.InteractiveInfo;
import org.apache.poi.hslf.record.InteractiveInfoAtom;
import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.usermodel.*;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;
/**
* For each slide iterate over shapes and found associated sound data.
*
* @author Yegor Kozlov
*/
public class SoundFinder {
public static void main(String[] args) throws Exception {
SlideShow ppt = new SlideShow(new FileInputStream(args[0]));
SoundData[] sounds = ppt.getSoundData();
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
Shape[] shape = slide[i].getShapes();
for (int j = 0; j < shape.length; j++) {
int soundRef = getSoundReference(shape[j]);
if(soundRef != -1) {
System.out.println("Slide["+i+"], shape["+j+"], soundRef: "+soundRef);
System.out.println(" " + sounds[soundRef].getSoundName());
System.out.println(" " + sounds[soundRef].getSoundType());
}
}
}
}
/**
* Check if a given shape is associated with a sound.
* @return 0-based reference to a sound in the sound collection
* or -1 if the shape is not associated with a sound
*/
protected static int getSoundReference(Shape shape){
int soundRef = -1;
//dive into the shape container and search for InteractiveInfoAtom
EscherContainerRecord spContainer = shape.getSpContainer();
List spchild = spContainer.getChildRecords();
for (Iterator it = spchild.iterator(); it.hasNext();) {
EscherRecord obj = (EscherRecord) it.next();
if (obj.getRecordId() == EscherClientDataRecord.RECORD_ID) {
byte[] data = obj.serialize();
Record[] records = Record.findChildRecords(data, 8,
data.length - 8);
for (int j = 0; j < records.length; j++) {
if (records[j] instanceof InteractiveInfo) {
InteractiveInfoAtom info = ((InteractiveInfo)records[j]).getInteractiveInfoAtom();
if (info.getAction() == InteractiveInfoAtom.ACTION_MEDIA) {
soundRef = info.getSoundRef();
}
}
}
}
}
return soundRef;
}
}

View File

@ -0,0 +1,127 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.examples;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.model.*;
import java.awt.*;
import java.io.FileOutputStream;
/**
* Demonstrates how to create tables
*
* @author Yegor Kozlov
*/
public final class TableDemo {
public static void main(String[] args) throws Exception {
//test data for the first taable
String[][] txt1 = {
{"INPUT FILE", "NUMBER OF RECORDS"},
{"Item File", "11,559"},
{"Vendor File", "502"},
{"Purchase History File - # of PO\u2019s\r(12/01/04 - 05/31/06)", "12,852"},
{"Purchase History File - # of PO Lines\r(12/01/04 - 05/31/06)", "53,523" },
{"Total PO History Spend", "$10,172,038"}
};
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
//six rows, two columns
Table table1 = new Table(6, 2);
for (int i = 0; i < txt1.length; i++) {
for (int j = 0; j < txt1[i].length; j++) {
TableCell cell = table1.getCell(i, j);
cell.setText(txt1[i][j]);
RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("Arial");
rt.setFontSize(10);
if(i == 0){
cell.getFill().setForegroundColor(new Color(227, 227, 227));
} else {
rt.setBold(true);
}
cell.setVerticalAlignment(TextBox.AnchorMiddle);
cell.setHorizontalAlignment(TextBox.AlignCenter);
}
}
Line border1 = table1.createBorder();
border1.setLineColor(Color.black);
border1.setLineWidth(1.0);
table1.setAllBorders(border1);
table1.setColumnWidth(0, 300);
table1.setColumnWidth(1, 150);
slide.addShape(table1);
int pgWidth = ppt.getPageSize().width;
table1.moveTo((pgWidth - table1.getAnchor().width)/2, 100);
//test data for the second taable
String[][] txt2 = {
{"Data Source"},
{"CAS Internal Metrics - Item Master Summary\r" +
"CAS Internal Metrics - Vendor Summary\r" +
"CAS Internal Metrics - PO History Summary"}
};
//two rows, one column
Table table2 = new Table(2, 1);
for (int i = 0; i < txt2.length; i++) {
for (int j = 0; j < txt2[i].length; j++) {
TableCell cell = table2.getCell(i, j);
cell.setText(txt2[i][j]);
RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontSize(10);
rt.setFontName("Arial");
if(i == 0){
cell.getFill().setForegroundColor(new Color(0, 51, 102));
rt.setFontColor(Color.white);
rt.setBold(true);
rt.setFontSize(14);
cell.setHorizontalAlignment(TextBox.AlignCenter);
} else {
rt.setBullet(true);
rt.setFontSize(12);
cell.setHorizontalAlignment(TextBox.AlignLeft);
}
cell.setVerticalAlignment(TextBox.AnchorMiddle);
}
}
table2.setColumnWidth(0, 300);
table2.setRowHeight(0, 30);
table2.setRowHeight(1, 70);
Line border2 = table2.createBorder();
table2.setOutsideBorders(border2);
slide.addShape(table2);
table2.moveTo(200, 400);
FileOutputStream out = new FileOutputStream("hslf-table.ppt");
ppt.write(out);
out.close();
}
}

View File

@ -0,0 +1,171 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hsmf.examples;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import org.apache.poi.hsmf.MAPIMessage;
import org.apache.poi.hsmf.datatypes.AttachmentChunks;
import org.apache.poi.hsmf.exceptions.ChunkNotFoundException;
/**
* Reads one or several Outlook MSG files and for each of them creates
* a text file from available chunks and a directory that contains
* attachments.
*
* @author Bruno Girin
*/
public class Msg2txt {
/**
* The stem used to create file names for the text file and the directory
* that contains the attachments.
*/
private String fileNameStem;
/**
* The Outlook MSG file being processed.
*/
private MAPIMessage msg;
public Msg2txt(String fileName) throws IOException {
fileNameStem = fileName;
if(fileNameStem.endsWith(".msg") || fileNameStem.endsWith(".MSG")) {
fileNameStem = fileNameStem.substring(0, fileNameStem.length() - 4);
}
msg = new MAPIMessage(fileName);
}
/**
* Processes the message.
*
* @throws IOException if an exception occurs while writing the message out
*/
public void processMessage() throws IOException {
String txtFileName = fileNameStem + ".txt";
String attDirName = fileNameStem + "-att";
PrintWriter txtOut = null;
try {
txtOut = new PrintWriter(txtFileName);
try {
String displayFrom = msg.getDisplayFrom();
txtOut.println("From: "+displayFrom);
} catch (ChunkNotFoundException e) {
// ignore
}
try {
String displayTo = msg.getDisplayTo();
txtOut.println("To: "+displayTo);
} catch (ChunkNotFoundException e) {
// ignore
}
try {
String displayCC = msg.getDisplayCC();
txtOut.println("CC: "+displayCC);
} catch (ChunkNotFoundException e) {
// ignore
}
try {
String displayBCC = msg.getDisplayBCC();
txtOut.println("BCC: "+displayBCC);
} catch (ChunkNotFoundException e) {
// ignore
}
try {
String subject = msg.getSubject();
txtOut.println("Subject: "+subject);
} catch (ChunkNotFoundException e) {
// ignore
}
try {
String body = msg.getTextBody();
txtOut.println(body);
} catch (ChunkNotFoundException e) {
System.err.println("No message body");
}
AttachmentChunks[] attachments = msg.getAttachmentFiles();
if(attachments.length > 0) {
File d = new File(attDirName);
if(d.mkdir()) {
for(AttachmentChunks attachment : attachments) {
processAttachment(attachment, d);
}
} else {
System.err.println("Can't create directory "+attDirName);
}
}
} finally {
if(txtOut != null) {
txtOut.close();
}
}
}
/**
* Processes a single attachment: reads it from the Outlook MSG file and
* writes it to disk as an individual file.
*
* @param attachment the chunk group describing the attachment
* @param dir the directory in which to write the attachment file
* @throws IOException when any of the file operations fails
*/
public void processAttachment(AttachmentChunks attachment,
File dir) throws IOException {
String fileName = attachment.attachFileName.toString();
if(attachment.attachLongFileName != null) {
fileName = attachment.attachLongFileName.toString();
}
File f = new File(dir, fileName);
OutputStream fileOut = null;
try {
fileOut = new FileOutputStream(f);
fileOut.write(attachment.attachData.getValue());
} finally {
if(fileOut != null) {
fileOut.close();
}
}
}
/**
* Processes the list of arguments as a list of names of Outlook MSG files.
*
* @param args the list of MSG files to process
*/
public static void main(String[] args) {
if(args.length <= 0) {
System.err.println("No files names provided");
} else {
for(int i = 0; i < args.length; i++) {
try {
Msg2txt processor = new Msg2txt(args[i]);
processor.processMessage();
} catch (IOException e) {
System.err.println("Could not process "+args[i]+": "+e);
}
}
}
}
}

View File

@ -0,0 +1,327 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.eventusermodel.examples;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.eventusermodel.MissingRecordAwareHSSFListener;
import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder.SheetRecordCollectingListener;
import org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord;
import org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord;
import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.BOFRecord;
import org.apache.poi.hssf.record.BlankRecord;
import org.apache.poi.hssf.record.BoolErrRecord;
import org.apache.poi.hssf.record.BoundSheetRecord;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.LabelRecord;
import org.apache.poi.hssf.record.LabelSSTRecord;
import org.apache.poi.hssf.record.NoteRecord;
import org.apache.poi.hssf.record.NumberRecord;
import org.apache.poi.hssf.record.RKRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.SSTRecord;
import org.apache.poi.hssf.record.StringRecord;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* A XLS -> CSV processor, that uses the MissingRecordAware
* EventModel code to ensure it outputs all columns and rows.
* @author Nick Burch
*/
public class XLS2CSVmra implements HSSFListener {
private int minColumns;
private POIFSFileSystem fs;
private PrintStream output;
private int lastRowNumber;
private int lastColumnNumber;
/** Should we output the formula, or the value it has? */
private boolean outputFormulaValues = true;
/** For parsing Formulas */
private SheetRecordCollectingListener workbookBuildingListener;
private HSSFWorkbook stubWorkbook;
// Records we pick up as we process
private SSTRecord sstRecord;
private FormatTrackingHSSFListener formatListener;
/** So we known which sheet we're on */
private int sheetIndex = -1;
private BoundSheetRecord[] orderedBSRs;
private ArrayList boundSheetRecords = new ArrayList();
// For handling formulas with string results
private int nextRow;
private int nextColumn;
private boolean outputNextStringRecord;
/**
* Creates a new XLS -> CSV converter
* @param fs The POIFSFileSystem to process
* @param output The PrintStream to output the CSV to
* @param minColumns The minimum number of columns to output, or -1 for no minimum
*/
public XLS2CSVmra(POIFSFileSystem fs, PrintStream output, int minColumns) {
this.fs = fs;
this.output = output;
this.minColumns = minColumns;
}
/**
* Creates a new XLS -> CSV converter
* @param filename The file to process
* @param minColumns The minimum number of columns to output, or -1 for no minimum
* @throws IOException
* @throws FileNotFoundException
*/
public XLS2CSVmra(String filename, int minColumns) throws IOException, FileNotFoundException {
this(
new POIFSFileSystem(new FileInputStream(filename)),
System.out, minColumns
);
}
/**
* Initiates the processing of the XLS file to CSV
*/
public void process() throws IOException {
MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
formatListener = new FormatTrackingHSSFListener(listener);
HSSFEventFactory factory = new HSSFEventFactory();
HSSFRequest request = new HSSFRequest();
if(outputFormulaValues) {
request.addListenerForAllRecords(formatListener);
} else {
workbookBuildingListener = new SheetRecordCollectingListener(formatListener);
request.addListenerForAllRecords(workbookBuildingListener);
}
factory.processWorkbookEvents(request, fs);
}
/**
* Main HSSFListener method, processes events, and outputs the
* CSV as the file is processed.
*/
public void processRecord(Record record) {
int thisRow = -1;
int thisColumn = -1;
String thisStr = null;
switch (record.getSid())
{
case BoundSheetRecord.sid:
boundSheetRecords.add(record);
break;
case BOFRecord.sid:
BOFRecord br = (BOFRecord)record;
if(br.getType() == BOFRecord.TYPE_WORKSHEET) {
// Create sub workbook if required
if(workbookBuildingListener != null && stubWorkbook == null) {
stubWorkbook = workbookBuildingListener.getStubHSSFWorkbook();
}
// Output the worksheet name
// Works by ordering the BSRs by the location of
// their BOFRecords, and then knowing that we
// process BOFRecords in byte offset order
sheetIndex++;
if(orderedBSRs == null) {
orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
}
output.println();
output.println(
orderedBSRs[sheetIndex].getSheetname() +
" [" + (sheetIndex+1) + "]:"
);
}
break;
case SSTRecord.sid:
sstRecord = (SSTRecord) record;
break;
case BlankRecord.sid:
BlankRecord brec = (BlankRecord) record;
thisRow = brec.getRow();
thisColumn = brec.getColumn();
thisStr = "";
break;
case BoolErrRecord.sid:
BoolErrRecord berec = (BoolErrRecord) record;
thisRow = berec.getRow();
thisColumn = berec.getColumn();
thisStr = "";
break;
case FormulaRecord.sid:
FormulaRecord frec = (FormulaRecord) record;
thisRow = frec.getRow();
thisColumn = frec.getColumn();
if(outputFormulaValues) {
if(Double.isNaN( frec.getValue() )) {
// Formula result is a string
// This is stored in the next record
outputNextStringRecord = true;
nextRow = frec.getRow();
nextColumn = frec.getColumn();
} else {
thisStr = formatListener.formatNumberDateCell(frec);
}
} else {
thisStr = '"' +
HSSFFormulaParser.toFormulaString(stubWorkbook, frec.getParsedExpression()) + '"';
}
break;
case StringRecord.sid:
if(outputNextStringRecord) {
// String for formula
StringRecord srec = (StringRecord)record;
thisStr = srec.getString();
thisRow = nextRow;
thisColumn = nextColumn;
outputNextStringRecord = false;
}
break;
case LabelRecord.sid:
LabelRecord lrec = (LabelRecord) record;
thisRow = lrec.getRow();
thisColumn = lrec.getColumn();
thisStr = '"' + lrec.getValue() + '"';
break;
case LabelSSTRecord.sid:
LabelSSTRecord lsrec = (LabelSSTRecord) record;
thisRow = lsrec.getRow();
thisColumn = lsrec.getColumn();
if(sstRecord == null) {
thisStr = '"' + "(No SST Record, can't identify string)" + '"';
} else {
thisStr = '"' + sstRecord.getString(lsrec.getSSTIndex()).toString() + '"';
}
break;
case NoteRecord.sid:
NoteRecord nrec = (NoteRecord) record;
thisRow = nrec.getRow();
thisColumn = nrec.getColumn();
// TODO: Find object to match nrec.getShapeId()
thisStr = '"' + "(TODO)" + '"';
break;
case NumberRecord.sid:
NumberRecord numrec = (NumberRecord) record;
thisRow = numrec.getRow();
thisColumn = numrec.getColumn();
// Format
thisStr = formatListener.formatNumberDateCell(numrec);
break;
case RKRecord.sid:
RKRecord rkrec = (RKRecord) record;
thisRow = rkrec.getRow();
thisColumn = rkrec.getColumn();
thisStr = '"' + "(TODO)" + '"';
break;
default:
break;
}
// Handle new row
if(thisRow != -1 && thisRow != lastRowNumber) {
lastColumnNumber = -1;
}
// Handle missing column
if(record instanceof MissingCellDummyRecord) {
MissingCellDummyRecord mc = (MissingCellDummyRecord)record;
thisRow = mc.getRow();
thisColumn = mc.getColumn();
thisStr = "";
}
// If we got something to print out, do so
if(thisStr != null) {
if(thisColumn > 0) {
output.print(',');
}
output.print(thisStr);
}
// Update column and row count
if(thisRow > -1)
lastRowNumber = thisRow;
if(thisColumn > -1)
lastColumnNumber = thisColumn;
// Handle end of row
if(record instanceof LastCellOfRowDummyRecord) {
// Print out any missing commas if needed
if(minColumns > 0) {
// Columns are 0 based
if(lastColumnNumber == -1) { lastColumnNumber = 0; }
for(int i=lastColumnNumber; i<(minColumns); i++) {
output.print(',');
}
}
// We're onto a new row
lastColumnNumber = -1;
// End the row
output.println();
}
}
public static void main(String[] args) throws Exception {
if(args.length < 1) {
System.err.println("Use:");
System.err.println(" XLS2CSVmra <xls file> [min columns]");
System.exit(1);
}
int minColumns = -1;
if(args.length >= 2) {
minColumns = Integer.parseInt(args[1]);
}
XLS2CSVmra xls2csv = new XLS2CSVmra(args[0], minColumns);
xls2csv.process();
}
}

View File

@ -0,0 +1,946 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.util.CellReference;
/**
* Demonstrates how to add an image to a worksheet and set that image's size
* to a specific number of milimetres irrespective of the width of the columns
* or height of the rows. Overridden methods are provided so that the location
* of the image - the cells row and column co-ordinates that define the top
* left hand corners of the image - can be identified either in the familiar
* Excel manner - A1 for instance - or using POI's methodolody of a column and
* row index where 0, 0 would indicate cell A1.
*
* The best way to make use of these techniques is to delay adding the image to
* the sheet until all other work has been completed. That way, the sizes of
* all rows and columns will have been adjusted - assuming that step was
* necessary. Even though the anchors type is set to prevent the image moving
* or re-sizing, this setting does not have any effect until the sheet is being
* viewed using the Excel application.
*
* The key to the process is the HSSFClientAnchor class. It accepts eight
* parameters that define, in order;
*
* * How far - in terms of co-ordinate position - the image should be inset
* from the left hand border of a cell.
* * How far - in terms of co-ordinate positions - the image should be inset
* from the from the top of the cell.
* * How far - in terms of co-ordinate positions - the right hand edge of
* the image should protrude into a cell (measured from the cell's left hand
* edge to the image's right hand edge).
* * How far - in terms of co-ordinate positions - the bottm edge of the
* image should protrude into a row (measured from the cell's top edge to
* the image's bottom edge).
* * The index of the column that contains the cell whose top left hand
* corner should be aligned with the top left hand corner of the image.
* * The index of the row that contains the cell whose top left hand corner
* should be aligned with the image's top left hand corner.
* * The index of the column that contains the cell whose top left hand
* corner should be aligned with the image's bottom right hand corner
* * The index number of the row that contains the cell whose top left
* hand corner should be aligned with the images bottom right hand corner.
*
* It can be used to add an image into cell A1, for example, in the following
* manner;
*
* HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0,
* (short)0, 0, (short)1, 1);
*
* The final four parameters determine that the top left hand corner should be
* aligned with the top left hand corner of cell A1 and it's bottom right
* hand corner with the top left hand corner of cell B2. Think of the image as
* being stretched so that it's top left hand corner is aligned with the top
* left hand corner of cell A1 and it's bottom right hand corner is aligned with
* the top left hand corner of cell B1. Interestingly, this would also produce
* the same results;
*
* anchor = new HSSFClientAnchor(0, 0, 1023, 255,
* (short)0, 0, (short)0, 0);
*
* Note that the final four parameters all contain the same value and seem to
* indicate that the images top left hand corner is aligned with the top left
* hand corner of cell A1 and that it's bottom right hand corner is also
* aligned with the top left hand corner of cell A1. Yet, running this code
* would see the image fully occupying cell A1. That is the result of the
* values passed to parameters three and four; these I have referred to as
* determing the images co-ordinates within the cell. They indicate that the
* image should occupy - in order - the full width of the column and the full
* height of the row.
*
* The co-ordinate values shown are the maxima; and they are independent of
* row height/column width and of the font used. Passing 255 will always result
* in the image occupying the full height of the row and passing 1023 will
* always result in the image occupying the full width of the column. They help
* in situations where an image is larger than a column/row and must overlap
* into the next column/row. Using them does mean, however, that it is often
* necessary to perform conversions between Excel's characters units, points,
* pixels and millimetres in order to establish how many rows/columns an image
* should occupy and just what the varous insets ought to be.
*
* Note that the first two parameters of the HSSFClientAchor classes constructor
* are not made use of in the code that follows. It would be fairly trivial
* however to extend these example further and provide methods that would centre
* an image within a cell or allow the user to specify that a plain border a
* fixed number of millimetres wide should wrap around the image. Those first
* two parameters would make this sort of functionality perfectly possible.
*
* Owing to the various conversions used, the actual size of the image may vary
* from that required; testing has so far found this to be in the region of
* plus or minus two millimetres. Most likely by modifying the way the
* calculations are performed - possibly using double(s) throughout and
* rounding the values at the correct point - it is likely that these errors
* could be reduced or removed.
*
* A note concerning Excels' image resizing behaviour. The HSSFClientAnchor
* class contains a method called setAnchorType(int) which can be used to
* determine how Excel will resize an image in reponse to the user increasing
* or decreasing the dimensions of the cell containing the image. There are
* three values that can be passed to this method; 0 = To move and size the
* image with the cell, 2 = To move but don't size the image with the cell,
* 3 = To prevent the image from moving or being resized along with the cell. If
* an image is inserted using this class and placed into a single cell then if
* the setAnchorType(int) method is called and a value of either 0 or 2 passed
* to it, the resultant resizing behaviour may be a surprise. The image will not
* grow in size of the column is made wider or the row higher but it will shrink
* if the columns width or rows height are reduced.
*
* @author Mark Beardsley [msb at apache.org]
* @version 1.00 5th August 2009.
*/
public class AddDimensionedImage {
// Four constants that determine how - and indeed whether - the rows
// and columns an image may overlie should be expanded to accomodate that
// image.
// Passing EXPAND_ROW will result in the height of a row being increased
// to accomodate the image if it is not already larger. The image will
// be layed across one or more columns.
// Passing EXPAND_COLUMN will result in the width of the column being
// increased to accomodate the image if it is not already larger. The image
// will be layed across one or many rows.
// Passing EXPAND_ROW_AND_COLUMN will result in the height of the row
// bing increased along with the width of the column to accomdate the
// image if either is not already larger.
// Passing OVERLAY_ROW_AND_COLUMN will result in the image being layed
// over one or more rows and columns. No row or column will be resized,
// instead, code will determine how many rows and columns the image should
// overlie.
public static final int EXPAND_ROW = 1;
public static final int EXPAND_COLUMN = 2;
public static final int EXPAND_ROW_AND_COLUMN = 3;
public static final int OVERLAY_ROW_AND_COLUMN = 7;
/**
* Add an image to a worksheet.
*
* @param cellNumber A String that contains the location of the cell whose
* top left hand corner should be aligned with the top
* left hand corner of the image; for example "A1", "A2"
* etc. This is to support the familiar Excel syntax.
* Whilst images are are not actually inserted into cells
* this provides a convenient method of indicating where
* the image should be positioned on the sheet.
* @param sheet A reference to the sheet that contains the cell referenced
* above.
* @param imageFile A String that encapsulates the name of and path to
* the image that is to be 'inserted into' the sheet.
* @param reqImageWidthMM A primitive double that contains the required
* width of the image in millimetres.
* @param reqImageHeightMM A primitive double that contains the required
* height of the image in millimetres.
* @param resizeBehaviour A primitive int whose value will determine how
* the code should react if the image is larger than
* the cell referenced by the cellNumber parameter.
* Four constants are provided to determine what
* should happen;
* AddDimensionedImage.EXPAND_ROW
* AddDimensionedImage.EXPAND_COLUMN
* AddDimensionedImage.EXPAND_ROW_AND_COLUMN
* AddDimensionedImage.OVERLAY_ROW_AND_COLUMN
* @throws java.io.FileNotFoundException If the file containing the image
* cannot be located.
* @throws java.io.IOException If a problem occurs whilst reading the file
* of image data.
* @throws java.lang.IllegalArgumentException If an invalid value is passed
* to the resizeBehaviour
* parameter.
*/
public void addImageToSheet(String cellNumber, HSSFSheet sheet,
String imageFile, double reqImageWidthMM, double reqImageHeightMM,
int resizeBehaviour) throws IOException, IllegalArgumentException {
// Convert the String into column and row indices then chain the
// call to the overridden addImageToSheet() method.
CellReference cellRef = new CellReference(cellNumber);
this.addImageToSheet(cellRef.getCol(), cellRef.getRow(), sheet,
imageFile, reqImageWidthMM, reqImageHeightMM,resizeBehaviour);
}
/**
* Add an image to a worksheet.
*
* @param colNumber A primitive int that contains the index number of a
* column on the worksheet; POI column indices are zero
* based. Together with the rowNumber parameter's value,
* this parameter identifies a cell on the worksheet. The
* image's top left hand corner will be aligned with the
* top left hand corner of this cell.
* @param rowNumber A primtive int that contains the index number of a row
* on the worksheet; POI row indices are zero based.
* Together with the rowNumber parameter's value, this
* parameter identifies a cell on the worksheet. The
* image's top left hand corner will be aligned with the
* top left hand corner of this cell.
* @param sheet A reference to the sheet that contains the cell identified
* by the two parameters above.
* @param imageFile A String that encapsulates the name of and path to
* the image that is to be 'inserted into' the sheet.
* @param reqImageWidthMM A primitive double that contains the required
* width of the image in millimetres.
* @param reqImageHeightMM A primitive double that contains the required
* height of the image in millimetres.
* @param resizeBehaviour A primitive int whose value will determine how
* the code should react if the image is larger than
* the cell referenced by the colNumber and
* rowNumber parameters. Four constants are provided
* to determine what should happen;
* AddDimensionedImage.EXPAND_ROW
* AddDimensionedImage.EXPAND_COLUMN
* AddDimensionedImage.EXPAND_ROW_AND_COLUMN
* AddDimensionedImage.OVERLAY_ROW_AND_COLUMN
* @throws java.io.FileNotFoundException If the file containing the image
* cannot be located.
* @throws java.io.IOException If a problem occurs whilst reading the file
* of image data.
* @throws java.lang.IllegalArgumentException If an invalid value is passed
* to the resizeBehaviour
* parameter.
*/
private void addImageToSheet(int colNumber, int rowNumber, HSSFSheet sheet,
String imageFile, double reqImageWidthMM, double reqImageHeightMM,
int resizeBehaviour) throws FileNotFoundException, IOException,
IllegalArgumentException {
HSSFRow row = null;
HSSFClientAnchor anchor = null;
HSSFPatriarch patriarch = null;
ClientAnchorDetail rowClientAnchorDetail = null;
ClientAnchorDetail colClientAnchorDetail = null;
// Validate the resizeBehaviour parameter.
if((resizeBehaviour != AddDimensionedImage.EXPAND_COLUMN) &&
(resizeBehaviour != AddDimensionedImage.EXPAND_ROW) &&
(resizeBehaviour != AddDimensionedImage.EXPAND_ROW_AND_COLUMN) &&
(resizeBehaviour != AddDimensionedImage.OVERLAY_ROW_AND_COLUMN)) {
throw new IllegalArgumentException("Invalid value passed to the " +
"resizeBehaviour parameter of AddDimensionedImage.addImageToSheet()");
}
// Call methods to calculate how the image and sheet should be
// manipulated to accomodate the image; columns and then rows.
colClientAnchorDetail = this.fitImageToColumns(sheet, colNumber,
reqImageWidthMM, resizeBehaviour);
rowClientAnchorDetail = this.fitImageToRows(sheet, rowNumber,
reqImageHeightMM, resizeBehaviour);
// Having determined if and how to resize the rows, columns and/or the
// image, create the HSSFClientAnchor object to position the image on
// the worksheet. Note how the two ClientAnchorDetail records are
// interrogated to recover the row/column co-ordinates and any insets.
// The first two parameters are not used currently but could be if the
// need arose to extend the functionality of this code by adding the
// ability to specify that a clear 'border' be placed around the image.
anchor = new HSSFClientAnchor(0,
0,
colClientAnchorDetail.getInset(),
rowClientAnchorDetail.getInset(),
(short)colClientAnchorDetail.getFromIndex(),
rowClientAnchorDetail.getFromIndex(),
(short)colClientAnchorDetail.getToIndex(),
rowClientAnchorDetail.getToIndex());
// For now, set the anchor type to do not move or resize the
// image as the size of the row/column is adjusted. This could easilly
// become another parameter passed to the method.
//anchor.setAnchorType(HSSFClientAnchor.DONT_MOVE_AND_RESIZE);
anchor.setAnchorType(HSSFClientAnchor.MOVE_AND_RESIZE);
// Now, add the picture to the workbook. Note that the type is assumed
// to be a JPEG/JPG, this could easily (and should) be parameterised
// however.
//int index = sheet.getWorkbook().addPicture(this.imageToBytes(imageFile),
// HSSFWorkbook.PICTURE_TYPE_JPEG);
int index = sheet.getWorkbook().addPicture(this.imageToBytes(imageFile), HSSFWorkbook.PICTURE_TYPE_PNG);
// Get the drawing patriarch and create the picture.
patriarch = sheet.createDrawingPatriarch();
patriarch.createPicture(anchor, index);
}
/**
* Determines whether the sheets columns should be re-sized to accomodate
* the image, adjusts the columns width if necessary and creates then
* returns a ClientAnchorDetail object that facilitates construction of
* an HSSFClientAnchor that will fix the image on the sheet and establish
* it's size.
*
* @param sheet A reference to the sheet that will 'contain' the image.
* @param colNumber A primtive int that contains the index number of a
* column on the sheet.
* @param reqImageWidthMM A primtive double that contains the required
* width of the image in millimetres
* @param resizeBehaviour A primitve int whose value will indicate how the
* width of the column should be adjusted if the
* required width of the image is greater than the
* width of the column.
* @return An instance of the ClientAnchorDetail class that will contain
* the index number of the column containing the cell whose top
* left hand corner also defines the top left hand corner of the
* image, the index number column containing the cell whose top
* left hand corner also defines the bottom right hand corner of
* the image and an inset that determines how far the right hand
* edge of the image can protrude into the next column - expressed
* as a specific number of co-ordinate positions.
*/
private ClientAnchorDetail fitImageToColumns(HSSFSheet sheet, int colNumber,
double reqImageWidthMM, int resizeBehaviour) {
double colWidthMM = 0.0D;
double colCoordinatesPerMM = 0.0D;
int pictureWidthCoordinates = 0;
ClientAnchorDetail colClientAnchorDetail = null;
// Get the colum's width in millimetres
colWidthMM = ConvertImageUnits.widthUnits2Millimetres(
(short)sheet.getColumnWidth(colNumber));
// Check that the column's width will accomodate the image at the
// required dimension. If the width of the column is LESS than the
// required width of the image, decide how the application should
// respond - resize the column or overlay the image across one or more
// columns.
if(colWidthMM < reqImageWidthMM) {
// Should the column's width simply be expanded?
if((resizeBehaviour == AddDimensionedImage.EXPAND_COLUMN) ||
(resizeBehaviour == AddDimensionedImage.EXPAND_ROW_AND_COLUMN)) {
// Set the width of the column by converting the required image
// width from millimetres into Excel's column width units.
sheet.setColumnWidth(colNumber,
ConvertImageUnits.millimetres2WidthUnits(reqImageWidthMM));
// To make the image occupy the full width of the column, convert
// the required width of the image into co-ordinates. This value
// will become the inset for the ClientAnchorDetail class that
// is then instantiated.
colWidthMM = reqImageWidthMM;
colCoordinatesPerMM = ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS /
colWidthMM;
pictureWidthCoordinates = (int)(reqImageWidthMM * colCoordinatesPerMM);
colClientAnchorDetail = new ClientAnchorDetail(colNumber,
colNumber, pictureWidthCoordinates);
}
// If the user has chosen to overlay both rows and columns or just
// to expand ONLY the size of the rows, then calculate how to lay
// the image out across one or more columns.
else if ((resizeBehaviour == AddDimensionedImage.OVERLAY_ROW_AND_COLUMN) ||
(resizeBehaviour == AddDimensionedImage.EXPAND_ROW)) {
colClientAnchorDetail = this.calculateColumnLocation(sheet,
colNumber, reqImageWidthMM);
}
}
// If the column is wider than the image.
else {
// Mow many co-ordinate positions are there per millimetre?
colCoordinatesPerMM = ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS /
colWidthMM;
// Given the width of the image, what should be it's co-ordinate?
pictureWidthCoordinates = (int)(reqImageWidthMM * colCoordinatesPerMM);
colClientAnchorDetail = new ClientAnchorDetail(colNumber,
colNumber, pictureWidthCoordinates);
}
return(colClientAnchorDetail);
}
/**
* Determines whether the sheet's row should be re-sized to accomodate
* the image, adjusts the rows height if necessary and creates then
* returns a ClientAnchorDetail object that facilitates construction of
* an HSSFClientAnchor that will fix the image on the sheet and establish
* it's size.
*
* @param sheet A reference to the sheet that will 'contain' the image.
* @param rowNumber A primtive int that contains the index number of a
* row on the sheet.
* @param reqImageHeightMM A primtive double that contains the required
* height of the image in millimetres
* @param resizeBehaviour A primitve int whose value will indicate how the
* height of the row should be adjusted if the
* required height of the image is greater than the
* height of the row.
* @return An instance of the ClientAnchorDetail class that will contain
* the index number of the row containing the cell whose top
* left hand corner also defines the top left hand corner of the
* image, the index number of the row containing the cell whose
* top left hand corner also defines the bottom right hand
* corner of the image and an inset that determines how far the
* bottom edge of the image can protrude into the next (lower)
* row - expressed as a specific number of co-ordinate positions.
*/
private ClientAnchorDetail fitImageToRows(HSSFSheet sheet, int rowNumber,
double reqImageHeightMM, int resizeBehaviour) {
HSSFRow row = null;
double rowHeightMM = 0.0D;
double rowCoordinatesPerMM = 0.0D;
int pictureHeightCoordinates = 0;
ClientAnchorDetail rowClientAnchorDetail = null;
// Get the row and it's height
row = sheet.getRow(rowNumber);
if(row == null) {
// Create row if it does not exist.
row = sheet.createRow(rowNumber);
}
// Get the row's height in millimetres
rowHeightMM = row.getHeightInPoints() / ConvertImageUnits.POINTS_PER_MILLIMETRE;
// Check that the row's height will accomodate the image at the required
// dimensions. If the height of the row is LESS than the required height
// of the image, decide how the application should respond - resize the
// row or overlay the image across a series of rows.
if(rowHeightMM < reqImageHeightMM) {
if((resizeBehaviour == AddDimensionedImage.EXPAND_ROW) ||
(resizeBehaviour == AddDimensionedImage.EXPAND_ROW_AND_COLUMN)) {
row.setHeightInPoints((float)(reqImageHeightMM *
ConvertImageUnits.POINTS_PER_MILLIMETRE));
rowHeightMM = reqImageHeightMM;
rowCoordinatesPerMM = ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS /
rowHeightMM;
pictureHeightCoordinates = (int)(reqImageHeightMM * rowCoordinatesPerMM);
rowClientAnchorDetail = new ClientAnchorDetail(rowNumber,
rowNumber, pictureHeightCoordinates);
}
// If the user has chosen to overlay both rows and columns or just
// to expand ONLY the size of the columns, then calculate how to lay
// the image out ver one or more rows.
else if((resizeBehaviour == AddDimensionedImage.OVERLAY_ROW_AND_COLUMN) ||
(resizeBehaviour == AddDimensionedImage.EXPAND_COLUMN)) {
rowClientAnchorDetail = this.calculateRowLocation(sheet,
rowNumber, reqImageHeightMM);
}
}
// Else, if the image is smaller than the space available
else {
rowCoordinatesPerMM = ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS /
rowHeightMM;
pictureHeightCoordinates = (int)(reqImageHeightMM * rowCoordinatesPerMM);
rowClientAnchorDetail = new ClientAnchorDetail(rowNumber,
rowNumber, pictureHeightCoordinates);
}
return(rowClientAnchorDetail);
}
/**
* If the image is to overlie more than one column, calculations need to be
* performed to determine how many columns and whether the image will
* overlie just a part of one column in order to be presented at the
* required size.
*
* @param sheet The sheet that will 'contain' the image.
* @param startingColumn A primitive int whose value is the index of the
* column that contains the cell whose top left hand
* corner should be aligned with the top left hand
* corner of the image.
* @param reqImageWidthMM A primitive double whose value will indicate the
* required width of the image in millimetres.
* @return An instance of the ClientAnchorDetail class that will contain
* the index number of the column containing the cell whose top
* left hand corner also defines the top left hand corner of the
* image, the index number column containing the cell whose top
* left hand corner also defines the bottom right hand corner of
* the image and an inset that determines how far the right hand
* edge of the image can protrude into the next column - expressed
* as a specific number of co-ordinate positions.
*/
private ClientAnchorDetail calculateColumnLocation(HSSFSheet sheet,
int startingColumn,
double reqImageWidthMM) {
ClientAnchorDetail anchorDetail = null;
double totalWidthMM = 0.0D;
double colWidthMM = 0.0D;
double overlapMM = 0.0D;
double coordinatePositionsPerMM = 0.0D;
int toColumn = startingColumn;
int inset = 0;
// Calculate how many columns the image will have to
// span in order to be presented at the required size.
while(totalWidthMM < reqImageWidthMM) {
colWidthMM = ConvertImageUnits.widthUnits2Millimetres(
(short)(sheet.getColumnWidth(toColumn)));
// Note use of the cell border width constant. Testing with an image
// declared to fit exactly into one column demonstrated that it's
// width was greater than the width of the column the POI returned.
// Further, this difference was a constant value that I am assuming
// related to the cell's borders. Either way, that difference needs
// to be allowed for in this calculation.
totalWidthMM += (colWidthMM + ConvertImageUnits.CELL_BORDER_WIDTH_MILLIMETRES);
toColumn++;
}
// De-crement by one the last column value.
toColumn--;
// Highly unlikely that this will be true but, if the width of a series
// of columns is exactly equal to the required width of the image, then
// simply build a ClientAnchorDetail object with an inset equal to the
// total number of co-ordinate positions available in a column, a
// from column co-ordinate (top left hand corner) equal to the value
// of the startingColumn parameter and a to column co-ordinate equal
// to the toColumn variable.
//
// Convert both values to ints to perform the test.
if((int)totalWidthMM == (int)reqImageWidthMM) {
// A problem could occur if the image is sized to fit into one or
// more columns. If that occurs, the value in the toColumn variable
// will be in error. To overcome this, there are two options, to
// ibcrement the toColumn variable's value by one or to pass the
// total number of co-ordinate positions to the third paramater
// of the ClientAnchorDetail constructor. For no sepcific reason,
// the latter option is used below.
anchorDetail = new ClientAnchorDetail(startingColumn,
toColumn, ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS);
}
// In this case, the image will overlap part of another column and it is
// necessary to calculate just how much - this will become the inset
// for the ClientAnchorDetail object.
else {
// Firstly, claculate how much of the image should overlap into
// the next column.
overlapMM = reqImageWidthMM - (totalWidthMM - colWidthMM);
// When the required size is very close indded to the column size,
// the calcaulation above can produce a negative value. To prevent
// problems occuring in later caculations, this is simply removed
// be setting the overlapMM value to zero.
if(overlapMM < 0) {
overlapMM = 0.0D;
}
// Next, from the columns width, calculate how many co-ordinate
// positons there are per millimetre
coordinatePositionsPerMM = ConvertImageUnits.TOTAL_COLUMN_COORDINATE_POSITIONS /
colWidthMM;
// From this figure, determine how many co-ordinat positions to
// inset the left hand or bottom edge of the image.
inset = (int)(coordinatePositionsPerMM * overlapMM);
// Now create the ClientAnchorDetail object, setting the from and to
// columns and the inset.
anchorDetail = new ClientAnchorDetail(startingColumn, toColumn, inset);
}
return(anchorDetail);
}
/**
* If the image is to overlie more than one rows, calculations need to be
* performed to determine how many rows and whether the image will
* overlie just a part of one row in order to be presented at the
* required size.
*
* @param sheet The sheet that will 'contain' the image.
* @param startingRow A primitive int whose value is the index of the row
* that contains the cell whose top left hand corner
* should be aligned with the top left hand corner of
* the image.
* @param reqImageHeightMM A primitive double whose value will indicate the
* required height of the image in millimetres.
* @return An instance of the ClientAnchorDetail class that will contain
* the index number of the row containing the cell whose top
* left hand corner also defines the top left hand corner of the
* image, the index number of the row containing the cell whose top
* left hand corner also defines the bottom right hand corner of
* the image and an inset that determines how far the bottom edge
* can protrude into the next (lower) row - expressed as a specific
* number of co-ordinate positions.
*/
private ClientAnchorDetail calculateRowLocation(HSSFSheet sheet,
int startingRow, double reqImageHeightMM) {
ClientAnchorDetail clientAnchorDetail = null;
HSSFRow row = null;
double rowHeightMM = 0.0D;
double totalRowHeightMM = 0.0D;
double overlapMM = 0.0D;
double rowCoordinatesPerMM = 0.0D;
int toRow = startingRow;
int inset = 0;
// Step through the rows in the sheet and accumulate a total of their
// heights.
while(totalRowHeightMM < reqImageHeightMM) {
row = sheet.getRow(toRow);
// Note, if the row does not already exist on the sheet then create
// it here.
if(row == null) {
row = sheet.createRow(toRow);
}
// Get the row's height in millimetres and add to the running total.
rowHeightMM = row.getHeightInPoints() /
ConvertImageUnits.POINTS_PER_MILLIMETRE;
totalRowHeightMM += rowHeightMM;
toRow++;
}
// Owing to the way the loop above works, the rowNumber will have been
// incremented one row too far. Undo that here.
toRow--;
// Check to see whether the image should occupy an exact number of
// rows. If so, build the ClientAnchorDetail record to point
// to those rows and with an inset of the total number of co-ordinate
// position in the row.
//
// To overcome problems that can occur with comparing double values for
// equality, cast both to int(s) to truncate the value; VERY crude and
// I do not really like it!!
if((int)totalRowHeightMM == (int)reqImageHeightMM) {
clientAnchorDetail = new ClientAnchorDetail(startingRow, toRow,
ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS);
}
else {
// Calculate how far the image will project into the next row. Note
// that the height of the last row assessed is subtracted from the
// total height of all rows assessed so far.
overlapMM = reqImageHeightMM - (totalRowHeightMM - rowHeightMM);
// To prevent an exception being thrown when the required width of
// the image is very close indeed to the column size.
if(overlapMM < 0) {
overlapMM = 0.0D;
}
rowCoordinatesPerMM = ConvertImageUnits.TOTAL_ROW_COORDINATE_POSITIONS /
rowHeightMM;
inset = (int)(overlapMM * rowCoordinatesPerMM);
clientAnchorDetail = new ClientAnchorDetail(startingRow,
toRow, inset);
}
return(clientAnchorDetail);
}
/**
* Loads - reads in and converts into an array of byte(s) - an image from
* a named file.
*
* Note: this method should be modified so that the type of the image may
* also be passed to it. Currently, it assumes that all images are
* JPG/JPEG(s).
*
* @param imageFilename A String that encapsulates the path to and name
* of the file that contains the image which is to be
* 'loaded'.
* @return An array of type byte that contains the raw data of the named
* image.
* @throws java.io.FileNotFoundException Thrown if it was not possible to
* open the specified file.
* @throws java.io.IOException Thrown if reading the file failed or was
* interrupted.
*/
private byte[] imageToBytes(String imageFilename) throws IOException {
File imageFile = null;
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
int read = 0;
try {
imageFile = new File(imageFilename);
fis = new FileInputStream(imageFile);
bos = new ByteArrayOutputStream();
while((read = fis.read()) != -1) {
bos.write(read);
}
return(bos.toByteArray());
}
finally {
if(fis != null) {
try {
fis.close();
fis = null;
}
catch(IOException ioEx) {
// Nothing to do here
}
}
}
}
/**
* The main entry point to the program. It contains code that demonstrates
* one way to use the program.
*
* Note, the code is not restricted to use on new workbooks only. If an
* image is to be inserted into an existing workbook. just open that
* workbook, gat a reference to a sheet and pass that;
*
* AddDimensionedImage addImage = new AddDimensionedImage();
*
* File file = new File("....... Existing Workbook .......");
* FileInputStream fis = new FileInputStream(file);
* HSSFWorkbook workbook = new HSSFWorkbook(fis);
* HSSFSheet sheet = workbook.getSheetAt(0);
* addImage.addImageToSheet("C3", sheet, "image.jpg", 30, 20,
* AddDimensionedImage.EXPAND.ROW);
*
* @param args the command line arguments
*/
public static void main(String[] args) {
String imageFile = null;
String outputFile = null;
FileInputStream fis = null;
FileOutputStream fos = null;
HSSFWorkbook workbook = null;
HSSFSheet sheet = null;
try {
if(args.length < 2){
System.err.println("Usage: AddDimensionedImage imageFile outputFile");
return;
}
imageFile = args[0];
outputFile = args[1];
workbook = new HSSFWorkbook();
sheet = workbook.createSheet("Picture Test");
new AddDimensionedImage().addImageToSheet("A1", sheet,
imageFile, 125, 125,
AddDimensionedImage.EXPAND_ROW_AND_COLUMN);
fos = new FileOutputStream(outputFile);
workbook.write(fos);
}
catch(FileNotFoundException fnfEx) {
System.out.println("Caught an: " + fnfEx.getClass().getName());
System.out.println("Message: " + fnfEx.getMessage());
System.out.println("Stacktrace follows...........");
fnfEx.printStackTrace(System.out);
}
catch(IOException ioEx) {
System.out.println("Caught an: " + ioEx.getClass().getName());
System.out.println("Message: " + ioEx.getMessage());
System.out.println("Stacktrace follows...........");
ioEx.printStackTrace(System.out);
}
finally {
if(fos != null) {
try {
fos.close();
fos = null;
}
catch(IOException ioEx) {
// I G N O R E
}
}
}
}
/**
* The HSSFClientAnchor class accepts eight parameters. In order, these are;
*
* * How far the left hand edge of the image is inset from the left hand
* edge of the cell
* * How far the top edge of the image is inset from the top of the cell
* * How far the right hand edge of the image is inset from the left
* hand edge of the cell
* * How far the bottom edge of the image is inset from the top of the
* cell.
* * Together, parameters five and six determine the column and row
* co-ordinates of the cell whose top left hand corner will be aligned
* with the image's top left hand corner.
* * Together, parameter seven and eight determine the column and row
* co-ordinates of the cell whose top left hand corner will be aligned
* with the images bottom right hand corner.
*
* An instance of the ClientAnchorDetail class provides three of the eight
* parameters, one of the co-ordinates for the images top left hand corner,
* one of the co-ordinates for the images bottom right hand corner and
* either how far the image should be inset from the top or the left hand
* edge of the cell.
*
* @author Mark Beardsley [mas at apache.org]
* @version 1.00 5th August 2009.
*/
public class ClientAnchorDetail {
public int fromIndex = 0;
public int toIndex = 0;
public int inset = 0;
/**
* Create a new instance of the ClientAnchorDetail class using the
* following parameters.
*
* @param fromIndex A primitive int that contains one of the
* co-ordinates (row or column index) for the top left
* hand corner of the image.
* @param toIndex A primitive int that contains one of the
* co-ordinates (row or column index) for the bottom
* right hand corner of the image.
* @param inset A primitive int that contains a value which indicates
* how far the image should be inset from the top or the
* left hand edge of a cell.
*/
public ClientAnchorDetail(int fromIndex, int toIndex, int inset) {
this.fromIndex = fromIndex;
this.toIndex = toIndex;
this.inset = inset;
}
/**
* Get one of the number of the column or row that contains the cell
* whose top left hand corner will be aligned with the top left hand
* corner of the image.
*
* @return The value - row or column index - for one of the co-ordinates
* of the top left hand corner of the image.
*/
public int getFromIndex() {
return(this.fromIndex);
}
/**
* Get one of the number of the column or row that contains the cell
* whose top left hand corner will be aligned with the bottom righ hand
* corner of the image.
*
* @return The value - row or column index - for one of the co-ordinates
* of the bottom right hand corner of the image.
*/
public int getToIndex() {
return(this.toIndex);
}
/**
* Get the image's offset from the edge of a cell.
*
* @return How far either the right hand or bottom edge of the image is
* inset from the left hand or top edge of a cell.
*/
public int getInset() {
return(this.inset);
}
}
/**
* Utility methods used to convert Excel's character based column and row
* size measurements into pixels and/or millimetres. The class also contains
* various constants that are required in other calculations.
*
* @author xio[darjino@hotmail.com]
* @version 1.01 30th July 2009.
* Added by Mark Beardsley [msb at apache.org].
* Additional constants.
* widthUnits2Millimetres() and millimetres2Units() methods.
*/
public static class ConvertImageUnits {
// Each cell conatins a fixed number of co-ordinate points; this number
// does not vary with row height or column width or with font. These two
// constants are defined below.
public static final int TOTAL_COLUMN_COORDINATE_POSITIONS = 1023; // MB
public static final int TOTAL_ROW_COORDINATE_POSITIONS = 255; // MB
// The resoultion of an image can be expressed as a specific number
// of pixels per inch. Displays and printers differ but 96 pixels per
// inch is an acceptable standard to beging with.
public static final int PIXELS_PER_INCH = 96; // MB
// Cnstants that defines how many pixels and points there are in a
// millimetre. These values are required for the conversion algorithm.
public static final double PIXELS_PER_MILLIMETRES = 3.78; // MB
public static final double POINTS_PER_MILLIMETRE = 2.83; // MB
// The column width returned by HSSF and the width of a picture when
// positioned to exactly cover one cell are different by almost exactly
// 2mm - give or take rounding errors. This constant allows that
// additional amount to be accounted for when calculating how many
// celles the image ought to overlie.
public static final double CELL_BORDER_WIDTH_MILLIMETRES = 2.0D; // MB
public static final short EXCEL_COLUMN_WIDTH_FACTOR = 256;
public static final int UNIT_OFFSET_LENGTH = 7;
public static final int[] UNIT_OFFSET_MAP = new int[]
{ 0, 36, 73, 109, 146, 182, 219 };
/**
* pixel units to excel width units(units of 1/256th of a character width)
* @param pxs
* @return
*/
public static short pixel2WidthUnits(int pxs) {
short widthUnits = (short) (EXCEL_COLUMN_WIDTH_FACTOR *
(pxs / UNIT_OFFSET_LENGTH));
widthUnits += UNIT_OFFSET_MAP[(pxs % UNIT_OFFSET_LENGTH)];
return widthUnits;
}
/**
* excel width units(units of 1/256th of a character width) to pixel
* units.
*
* @param widthUnits
* @return
*/
public static int widthUnits2Pixel(short widthUnits) {
int pixels = (widthUnits / EXCEL_COLUMN_WIDTH_FACTOR)
* UNIT_OFFSET_LENGTH;
int offsetWidthUnits = widthUnits % EXCEL_COLUMN_WIDTH_FACTOR;
pixels += Math.round((float) offsetWidthUnits /
((float) EXCEL_COLUMN_WIDTH_FACTOR / UNIT_OFFSET_LENGTH));
return pixels;
}
/**
* Convert Excel's width units into millimetres.
*
* @param widthUnits The width of the column or the height of the
* row in Excel's units.
* @return A primitive double that contains the columns width or rows
* height in millimetres.
*/
public static double widthUnits2Millimetres(short widthUnits) {
return(ConvertImageUnits.widthUnits2Pixel(widthUnits) /
ConvertImageUnits.PIXELS_PER_MILLIMETRES);
}
/**
* Convert into millimetres Excel's width units..
*
* @param millimetres A primitive double that contains the columns
* width or rows height in millimetres.
* @return A primitive int that contains the columns width or rows
* height in Excel's units.
*/
public static int millimetres2WidthUnits(double millimetres) {
return(ConvertImageUnits.pixel2WidthUnits((int)(millimetres *
ConvertImageUnits.PIXELS_PER_MILLIMETRES)));
}
}
}

View File

@ -0,0 +1,66 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Shows how various alignment options work.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class Alignment {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow(2);
createCell(wb, row, 0, HSSFCellStyle.ALIGN_CENTER);
createCell(wb, row, 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
createCell(wb, row, 2, HSSFCellStyle.ALIGN_FILL);
createCell(wb, row, 3, HSSFCellStyle.ALIGN_GENERAL);
createCell(wb, row, 4, HSSFCellStyle.ALIGN_JUSTIFY);
createCell(wb, row, 5, HSSFCellStyle.ALIGN_LEFT);
createCell(wb, row, 6, HSSFCellStyle.ALIGN_RIGHT);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
/**
* Creates a cell and aligns it a certain way.
*
* @param wb the workbook
* @param row the row to create the cell in
* @param column the column number to create the cell in
* @param align the alignment for the cell.
*/
private static void createCell(HSSFWorkbook wb, HSSFRow row, int column, int align) {
HSSFCell cell = row.createCell(column);
cell.setCellValue("Align It");
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment((short)align);
cell.setCellStyle(cellStyle);
}
}

View File

@ -0,0 +1,169 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Demonstrates many features of the user API at once. Used in the HOW-TO guide.
*
* @author Glen Stampoultzis (glens at apache.org)
* @author Andrew Oliver (acoliver at apache.org)
*/
public class BigExample {
public static void main(String[] args) throws IOException {
int rownum;
// create a new file
FileOutputStream out = new FileOutputStream("workbook.xls");
// create a new workbook
HSSFWorkbook wb = new HSSFWorkbook();
// create a new sheet
HSSFSheet s = wb.createSheet();
// declare a row object reference
HSSFRow r = null;
// declare a cell object reference
HSSFCell c = null;
// create 3 cell styles
HSSFCellStyle cs = wb.createCellStyle();
HSSFCellStyle cs2 = wb.createCellStyle();
HSSFCellStyle cs3 = wb.createCellStyle();
// create 2 fonts objects
HSSFFont f = wb.createFont();
HSSFFont f2 = wb.createFont();
//set font 1 to 12 point type
f.setFontHeightInPoints((short) 12);
//make it red
f.setColor(HSSFColor.RED.index);
// make it bold
//arial is the default font
f.setBoldweight(f.BOLDWEIGHT_BOLD);
//set font 2 to 10 point type
f2.setFontHeightInPoints((short) 10);
//make it the color at palette index 0xf (white)
f2.setColor(HSSFColor.WHITE.index);
//make it bold
f2.setBoldweight(f2.BOLDWEIGHT_BOLD);
//set cell stlye
cs.setFont(f);
//set the cell format see HSSFDataFromat for a full list
cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
//set a thin border
cs2.setBorderBottom(cs2.BORDER_THIN);
//fill w fg fill color
cs2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
// set foreground fill to red
cs2.setFillForegroundColor(HSSFColor.RED.index);
// set the font
cs2.setFont(f2);
// set the sheet name to HSSF Test
wb.setSheetName(0, "HSSF Test");
// create a sheet with 300 rows (0-299)
for (rownum = 0; rownum < 300; rownum++)
{
// create a row
r = s.createRow(rownum);
// on every other row
if ((rownum % 2) == 0)
{
// make the row height bigger (in twips - 1/20 of a point)
r.setHeight((short) 0x249);
}
//r.setRowNum(( short ) rownum);
// create 50 cells (0-49) (the += 2 becomes apparent later
for (int cellnum = 0; cellnum < 50; cellnum += 2)
{
// create a numeric cell
c = r.createCell(cellnum);
// do some goofy math to demonstrate decimals
c.setCellValue(rownum * 10000 + cellnum
+ (((double) rownum / 1000)
+ ((double) cellnum / 10000)));
// on every other row
if ((rownum % 2) == 0)
{
// set this cell to the first cell style we defined
c.setCellStyle(cs);
}
// create a string cell (see why += 2 in the
c = r.createCell(cellnum + 1);
// set the cell's string value to "TEST"
c.setCellValue("TEST");
// make this column a bit wider
s.setColumnWidth(cellnum + 1, (int)((50 * 8) / ((double) 1 / 20)));
// on every other row
if ((rownum % 2) == 0)
{
// set this to the white on red cell style
// we defined above
c.setCellStyle(cs2);
}
}
}
//draw a thick black border on the row at the bottom using BLANKS
// advance 2 rows
rownum++;
rownum++;
r = s.createRow(rownum);
// define the third style to be the default
// except with a thick black border at the bottom
cs3.setBorderBottom(cs3.BORDER_THICK);
//create 50 cells
for (int cellnum =0; cellnum < 50; cellnum++) {
//create a blank type cell (no value)
c = r.createCell(cellnum);
// set it to the thick black border style
c.setCellStyle(cs3);
}
//end draw thick black border
// demonstrate adding/naming and deleting a sheet
// create a sheet, set its title then delete it
s = wb.createSheet();
wb.setSheetName(1, "DeletedSheet");
wb.removeSheetAt(1);
//end deleted sheet
// write the workbook to the output stream
// close our file (don't blow out our file handles
wb.write(out);
out.close();
}
}

View File

@ -0,0 +1,60 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Demonstrates how to create borders around cells.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class Borders {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow(1);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell(1);
cell.setCellValue(4);
// Style the cell with borders all around.
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.GREEN.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLUE.index);
style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
style.setTopBorderColor(HSSFColor.ORANGE.index);
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,98 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.*;
/**
* Demonstrates how to work with excel cell comments.
*
* <p>
* Excel comment is a kind of a text shape,
* so inserting a comment is very similar to placing a text box in a worksheet
* </p>
*
* @author Yegor Kozlov
*/
public class CellComments {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Cell comments in POI HSSF");
// Create the drawing patriarch. This is the top level container for all shapes including cell comments.
HSSFPatriarch patr = sheet.createDrawingPatriarch();
//create a cell in row 3
HSSFCell cell1 = sheet.createRow(3).createCell(1);
cell1.setCellValue(new HSSFRichTextString("Hello, World"));
//anchor defines size and position of the comment in worksheet
HSSFComment comment1 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 2, (short) 6, 5));
// set text in the comment
comment1.setString(new HSSFRichTextString("We can set comments in POI"));
//set comment author.
//you can see it in the status bar when moving mouse over the commented cell
comment1.setAuthor("Apache Software Foundation");
// The first way to assign comment to a cell is via HSSFCell.setCellComment method
cell1.setCellComment(comment1);
//create another cell in row 6
HSSFCell cell2 = sheet.createRow(6).createCell(1);
cell2.setCellValue(36.6);
HSSFComment comment2 = patr.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short)4, 8, (short) 6, 11));
//modify background color of the comment
comment2.setFillColor(204, 236, 255);
HSSFRichTextString string = new HSSFRichTextString("Normal body temperature");
//apply custom font to the text in the comment
HSSFFont font = wb.createFont();
font.setFontName("Arial");
font.setFontHeightInPoints((short)10);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setColor(HSSFColor.RED.index);
string.applyFont(font);
comment2.setString(string);
comment2.setVisible(true); //by default comments are hidden. This one is always visible.
comment2.setAuthor("Bill Gates");
/**
* The second way to assign comment to a cell is to implicitly specify its row and column.
* Note, it is possible to set row and column of a non-existing cell.
* It works, the comment is visible.
*/
comment2.setRow(6);
comment2.setColumn(1);
FileOutputStream out = new FileOutputStream("poi_comment.xls");
wb.write(out);
out.close();
}
}

View File

@ -0,0 +1,45 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
public class CellTypes {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow(2);
row.createCell(0).setCellValue(1.1);
row.createCell(1).setCellValue(new Date());
row.createCell(2).setCellValue("a string");
row.createCell(3).setCellValue(true);
row.createCell(4).setCellType(HSSFCell.CELL_TYPE_ERROR);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,54 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Illustrates how to create cell values.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class CreateCells {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow(0);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell(0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue("This is a string");
row.createCell(3).setCellValue(true);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,58 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
/**
* An example on how to cells with dates. The important thing to note
* about dates is that they are really normal numeric cells that are
* formatted specially.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class CreateDateCells {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow(0);
// Create a cell and put a date value in it. The first cell is not styled as a date.
HSSFCell cell = row.createCell(0);
cell.setCellValue(new Date());
// we style the second cell as a date (and time). It is important to create a new cell style from the workbook
// otherwise you can end up modifying the built in style and effecting not only this cell but other cells.
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,68 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.SlideShow;
import java.io.FileInputStream;
import java.util.Iterator;
/**
* Demonstrates how you can extract embedded data from a .xls file
*/
public class EmeddedObjects {
public static void main(String[] args) throws Exception {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(args[0]));
HSSFWorkbook workbook = new HSSFWorkbook(fs);
for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {
//the OLE2 Class Name of the object
String oleName = obj.getOLE2ClassName();
if (oleName.equals("Worksheet")) {
DirectoryNode dn = (DirectoryNode) obj.getDirectory();
HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(dn, fs, false);
//System.out.println(entry.getName() + ": " + embeddedWorkbook.getNumberOfSheets());
} else if (oleName.equals("Document")) {
DirectoryNode dn = (DirectoryNode) obj.getDirectory();
HWPFDocument embeddedWordDocument = new HWPFDocument(dn);
//System.out.println(entry.getName() + ": " + embeddedWordDocument.getRange().text());
} else if (oleName.equals("Presentation")) {
DirectoryNode dn = (DirectoryNode) obj.getDirectory();
SlideShow embeddedPowerPointDocument = new SlideShow(new HSLFSlideShow(dn));
//System.out.println(entry.getName() + ": " + embeddedPowerPointDocument.getSlides().length);
} else {
if(obj.hasDirectoryEntry()){
// The DirectoryEntry is a DocumentNode. Examine its entries to find out what it is
DirectoryNode dn = (DirectoryNode) obj.getDirectory();
for (Iterator entries = dn.getEntries(); entries.hasNext();) {
Entry entry = (Entry) entries.next();
//System.out.println(oleName + "." + entry.getName());
}
} else {
// There is no DirectoryEntry
// Recover the object's data from the HSSFObjectData instance.
byte[] objectData = obj.getObjectData();
}
}
}
}
}

View File

@ -0,0 +1,118 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.record.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* This example shows how to use the event API for reading a file.
*/
public class EventExample
implements HSSFListener
{
private SSTRecord sstrec;
/**
* This method listens for incoming records and handles them as required.
* @param record The record that was found while reading.
*/
public void processRecord(Record record)
{
switch (record.getSid())
{
// the BOFRecord can represent either the beginning of a sheet or the workbook
case BOFRecord.sid:
BOFRecord bof = (BOFRecord) record;
if (bof.getType() == bof.TYPE_WORKBOOK)
{
System.out.println("Encountered workbook");
// assigned to the class level member
} else if (bof.getType() == bof.TYPE_WORKSHEET)
{
System.out.println("Encountered sheet reference");
}
break;
case BoundSheetRecord.sid:
BoundSheetRecord bsr = (BoundSheetRecord) record;
System.out.println("New sheet named: " + bsr.getSheetname());
break;
case RowRecord.sid:
RowRecord rowrec = (RowRecord) record;
System.out.println("Row found, first column at "
+ rowrec.getFirstCol() + " last column at " + rowrec.getLastCol());
break;
case NumberRecord.sid:
NumberRecord numrec = (NumberRecord) record;
System.out.println("Cell found with value " + numrec.getValue()
+ " at row " + numrec.getRow() + " and column " + numrec.getColumn());
break;
// SSTRecords store a array of unique strings used in Excel.
case SSTRecord.sid:
sstrec = (SSTRecord) record;
for (int k = 0; k < sstrec.getNumUniqueStrings(); k++)
{
System.out.println("String table value " + k + " = " + sstrec.getString(k));
}
break;
case LabelSSTRecord.sid:
LabelSSTRecord lrec = (LabelSSTRecord) record;
System.out.println("String cell found with value "
+ sstrec.getString(lrec.getSSTIndex()));
break;
}
}
/**
* Read an excel file and spit out what we find.
*
* @param args Expect one argument that is the file to read.
* @throws IOException When there is an error processing the file.
*/
public static void main(String[] args) throws IOException
{
// create a new file input stream with the input file specified
// at the command line
FileInputStream fin = new FileInputStream(args[0]);
// create a new org.apache.poi.poifs.filesystem.Filesystem
POIFSFileSystem poifs = new POIFSFileSystem(fin);
// get the Workbook (excel part) stream in a InputStream
InputStream din = poifs.createDocumentInputStream("Workbook");
// construct out HSSFRequest object
HSSFRequest req = new HSSFRequest();
// lazy listen for ALL records with the listener shown above
req.addListenerForAllRecords(new EventExample());
// create our event factory
HSSFEventFactory factory = new HSSFEventFactory();
// process our events based on the document input stream
factory.processEvents(req, din);
// once all the events are processed close our file input stream
fin.close();
// and our document input stream (don't want to leak these!)
din.close();
System.out.println("done.");
}
}

View File

@ -0,0 +1,60 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Shows how to use various fills.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class FrillsAndFills {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow(1);
// Aqua background
HSSFCellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(HSSFColor.AQUA.index);
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
HSSFCell cell = row.createCell(1);
cell.setCellValue("X");
cell.setCellStyle(style);
// Orange "foreground", foreground being the fill foreground not the font color.
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.ORANGE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cell = row.createCell(2);
cell.setCellValue("X");
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,244 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;
/**
* File for HSSF testing/examples
*
* THIS IS NOT THE MAIN HSSF FILE!! This is a utility for testing functionality.
* It does contain sample API usage that may be educational to regular API
* users.
*
* @see #main
* @author Andrew Oliver (acoliver at apache dot org)
*/
public final class HSSFReadWrite {
/**
* creates an {@link HSSFWorkbook} the specified OS filename.
*/
private static HSSFWorkbook readFile(String filename) throws IOException {
return new HSSFWorkbook(new FileInputStream(filename));
}
/**
* given a filename this outputs a sample sheet with just a set of
* rows/cells.
*/
private static void testCreateSampleSheet(String outputFilename) throws IOException {
int rownum;
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFCellStyle cs = wb.createCellStyle();
HSSFCellStyle cs2 = wb.createCellStyle();
HSSFCellStyle cs3 = wb.createCellStyle();
HSSFFont f = wb.createFont();
HSSFFont f2 = wb.createFont();
f.setFontHeightInPoints((short) 12);
f.setColor((short) 0xA);
f.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
f2.setFontHeightInPoints((short) 10);
f2.setColor((short) 0xf);
f2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cs.setFont(f);
cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
cs2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cs2.setFillPattern((short) 1); // fill w fg
cs2.setFillForegroundColor((short) 0xA);
cs2.setFont(f2);
wb.setSheetName(0, "HSSF Test");
for (rownum = 0; rownum < 300; rownum++) {
HSSFRow r = s.createRow(rownum);
if ((rownum % 2) == 0) {
r.setHeight((short) 0x249);
}
for (int cellnum = 0; cellnum < 50; cellnum += 2) {
HSSFCell c = r.createCell(cellnum);
c.setCellValue(rownum * 10000 + cellnum
+ (((double) rownum / 1000) + ((double) cellnum / 10000)));
if ((rownum % 2) == 0) {
c.setCellStyle(cs);
}
c = r.createCell(cellnum + 1);
c.setCellValue(new HSSFRichTextString("TEST"));
// 50 characters divided by 1/20th of a point
s.setColumnWidth(cellnum + 1, (int) (50 * 8 / 0.05));
if ((rownum % 2) == 0) {
c.setCellStyle(cs2);
}
}
}
// draw a thick black border on the row at the bottom using BLANKS
rownum++;
rownum++;
HSSFRow r = s.createRow(rownum);
cs3.setBorderBottom(HSSFCellStyle.BORDER_THICK);
for (int cellnum = 0; cellnum < 50; cellnum++) {
HSSFCell c = r.createCell(cellnum);
c.setCellStyle(cs3);
}
s.addMergedRegion(new CellRangeAddress(0, 3, 0, 3));
s.addMergedRegion(new CellRangeAddress(100, 110, 100, 110));
// end draw thick black border
// create a sheet, set its title then delete it
s = wb.createSheet();
wb.setSheetName(1, "DeletedSheet");
wb.removeSheetAt(1);
// end deleted sheet
FileOutputStream out = new FileOutputStream(outputFilename);
wb.write(out);
out.close();
}
/**
* Method main
*
* Given 1 argument takes that as the filename, inputs it and dumps the
* cell values/types out to sys.out.<br/>
*
* given 2 arguments where the second argument is the word "write" and the
* first is the filename - writes out a sample (test) spreadsheet
* see {@link HSSFReadWrite#testCreateSampleSheet(String)}.<br/>
*
* given 2 arguments where the first is an input filename and the second
* an output filename (not write), attempts to fully read in the
* spreadsheet and fully write it out.<br/>
*
* given 3 arguments where the first is an input filename and the second an
* output filename (not write) and the third is "modify1", attempts to read in the
* spreadsheet, deletes rows 0-24, 74-99. Changes cell at row 39, col 3 to
* "MODIFIED CELL" then writes it out. Hence this is "modify test 1". If you
* take the output from the write test, you'll have a valid scenario.
*/
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("At least one argument expected");
return;
}
String fileName = args[0];
try {
if (args.length < 2) {
HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
System.out.println("Data dump:\n");
for (int k = 0; k < wb.getNumberOfSheets(); k++) {
HSSFSheet sheet = wb.getSheetAt(k);
int rows = sheet.getPhysicalNumberOfRows();
System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows
+ " row(s).");
for (int r = 0; r < rows; r++) {
HSSFRow row = sheet.getRow(r);
if (row == null) {
continue;
}
int cells = row.getPhysicalNumberOfCells();
System.out.println("\nROW " + row.getRowNum() + " has " + cells
+ " cell(s).");
for (int c = 0; c < cells; c++) {
HSSFCell cell = row.getCell(c);
String value = null;
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
value = "FORMULA value=" + cell.getCellFormula();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
value = "NUMERIC value=" + cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING:
value = "STRING value=" + cell.getStringCellValue();
break;
default:
}
System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE="
+ value);
}
}
}
} else if (args.length == 2) {
if (args[1].toLowerCase().equals("write")) {
System.out.println("Write mode");
long time = System.currentTimeMillis();
HSSFReadWrite.testCreateSampleSheet(fileName);
System.out.println("" + (System.currentTimeMillis() - time)
+ " ms generation time");
} else {
System.out.println("readwrite test");
HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
FileOutputStream stream = new FileOutputStream(args[1]);
wb.write(stream);
stream.close();
}
} else if (args.length == 3 && args[2].toLowerCase().equals("modify1")) {
// delete row 0-24, row 74 - 99 && change cell 3 on row 39 to string "MODIFIED CELL!!"
HSSFWorkbook wb = HSSFReadWrite.readFile(fileName);
FileOutputStream stream = new FileOutputStream(args[1]);
HSSFSheet sheet = wb.getSheetAt(0);
for (int k = 0; k < 25; k++) {
HSSFRow row = sheet.getRow(k);
sheet.removeRow(row);
}
for (int k = 74; k < 100; k++) {
HSSFRow row = sheet.getRow(k);
sheet.removeRow(row);
}
HSSFRow row = sheet.getRow(39);
HSSFCell cell = row.getCell(3);
cell.setCellValue("MODIFIED CELL!!!!!");
wb.write(stream);
stream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,44 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Test if hyperlink formula, with url that got more than 127 characters, works
*
* @author Bernard Chesnoy
*/
public class HyperlinkFormula {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
cell.setCellFormula("HYPERLINK(\"http://127.0.0.1:8080/toto/truc/index.html?test=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\", \"test\")");
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,89 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.IOException;
import java.io.FileOutputStream;
/**
* Demonstrates how to create hyperlinks.
*
* @author Yegor Kozlov (yegor at apach.org)
*/
public class Hyperlinks {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
//cell style for hyperlinks
//by default hyperlinks are blue and underlined
HSSFCellStyle hlink_style = wb.createCellStyle();
HSSFFont hlink_font = wb.createFont();
hlink_font.setUnderline(HSSFFont.U_SINGLE);
hlink_font.setColor(HSSFColor.BLUE.index);
hlink_style.setFont(hlink_font);
HSSFCell cell;
HSSFSheet sheet = wb.createSheet("Hyperlinks");
//URL
cell = sheet.createRow(0).createCell(0);
cell.setCellValue("URL Link");
HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
link.setAddress("http://poi.apache.org/");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
//link to a file in the current directory
cell = sheet.createRow(1).createCell(0);
cell.setCellValue("File Link");
link = new HSSFHyperlink(HSSFHyperlink.LINK_FILE);
link.setAddress("link1.xls");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
//e-mail link
cell = sheet.createRow(2).createCell(0);
cell.setCellValue("Email Link");
link = new HSSFHyperlink(HSSFHyperlink.LINK_EMAIL);
//note, if subject contains white spaces, make sure they are url-encoded
link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
//link to a place in this workbook
//create a target sheet and cell
HSSFSheet sheet2 = wb.createSheet("Target Sheet");
sheet2.createRow(0).createCell(0).setCellValue("Target Cell");
cell = sheet.createRow(3).createCell(0);
cell.setCellValue("Worksheet Link");
link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);
link.setAddress("'Target Sheet'!A1");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
FileOutputStream out = new FileOutputStream("hssf-links.xls");
wb.write(out);
out.close();
}
}

View File

@ -0,0 +1,569 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
/**
* This class contains code that demonstrates how to insert plain, numbered
* and bulleted lists into an Excel spreadsheet cell.
*
* Look at the code contained in the demonstrateMethodCalls() method. It calls
* other methods that create plain, numbered and bulleted single and
* multi-level lists. The demonstrateMethodCalls() method appears at the top
* of the class definition.
*
* Though different methods are provided to construct single and multi-level
* plain, numbered and bulleted lists, close examination will reveal that they
* are not strictly necessary. If the inputs to the listInCell() and
* multilLevelListInCell() methods are constructed to include the bullet
* character or the item numbers then these methods alone may be sufficient.
*
* @author Mark Beardsley [msb at apache.org]
*/
public class InCellLists {
// This character looks like a solid, black, loser case letter 'o'
// positioned up from the base line of the text.
private static final char BULLET_CHARACTER = '\u2022';
// The tab character - \t - cannot be used to create a tab space
// within a cell as it is rendered as a square. Therefore, four
// spaces are used to simulate that character.
private static final String TAB = " ";
/**
* Call each of the list creation methods.
*
* @param outputFilename A String that encapsulates the name of and path to
* the Excel spreadsheet file this code will create.
*/
public void demonstrateMethodCalls(String outputFilename) {
HSSFWorkbook workbook = null;
HSSFSheet sheet = null;
HSSFRow row = null;
HSSFCell cell = null;
File outputFile = null;
FileOutputStream fos = null;
ArrayList<MultiLevelListItem> multiLevelListItems = null;
ArrayList<String> listItems = null;
String listItem = null;
try {
workbook = new HSSFWorkbook();
sheet = workbook.createSheet("In Cell Lists");
row = sheet.createRow(0);
// Create a cell at A1 and insert a single, bulleted, item into
// that cell.
cell = row.createCell(0);
this.bulletedItemInCell(workbook, "List Item", cell);
// Create a cell at A2 and insert a plain list - that is one
// whose items are neither bulleted or numbered - into that cell.
row = sheet.createRow(1);
cell = row.createCell(0);
listItems = new ArrayList<String>();
listItems.add("List Item One.");
listItems.add("List Item Two.");
listItems.add("List Item Three.");
listItems.add("List Item Four.");
this.listInCell(workbook, listItems, cell);
// The row height and cell width are set here to ensure that the
// list may be seen.
row.setHeight((short)1100);
sheet.setColumnWidth(0, 9500);
// Create a cell at A3 and insert a numbered list into that cell.
// Note that a couple of items have been added to the listItems
// ArrayList
row = sheet.createRow(2);
cell = row.createCell(0);
listItems.add("List Item Five.");
listItems.add("List Item Six.");
this.numberedListInCell(workbook, listItems, cell, 1, 2);
row.setHeight((short)1550);
// Create a cell at A4 and insert a numbered list into that cell.
// Note that a couple of items have been added to the listItems
// ArrayList
row = sheet.createRow(3);
cell = row.createCell(0);
listItems.add("List Item Seven.");
listItems.add("List Item Eight.");
listItems.add("List Item Nine.");
listItems.add("List Item Ten.");
this.bulletedListInCell(workbook, listItems, cell);
row.setHeight((short)2550);
// Insert a plain, multi-level list into cell A5. Note that
// the major difference here is that the list items are passed as
// an ArrayList of MultiLevelListItems. Note that an ArrayList
// of instances of an inner class was used here in preference to
// a Hashtable or HashMap as the ArrayList will preserve the
// ordering of the items added to it; the first item added will
// be the first item recovered and the last item added, the last
// item recovered.
row = sheet.createRow(4);
cell = row.createCell(0);
multiLevelListItems = new ArrayList<MultiLevelListItem>();
listItems = new ArrayList<String>();
listItems.add("ML List Item One - Sub Item One.");
listItems.add("ML List Item One - Sub Item Two.");
listItems.add("ML List Item One - Sub Item Three.");
listItems.add("ML List Item One - Sub Item Four.");
multiLevelListItems.add(new MultiLevelListItem("List Item One.", listItems));
// Passing either null or an empty ArrayList will signal that
// there are no lower level items associated with the top level
// item
multiLevelListItems.add(new MultiLevelListItem("List Item Two.", null));
multiLevelListItems.add(new MultiLevelListItem("List Item Three.", null));
listItems = new ArrayList<String>();
listItems.add("ML List Item Four - Sub Item One.");
listItems.add("ML List Item Four - Sub Item Two.");
listItems.add("ML List Item Four - Sub Item Three.");
multiLevelListItems.add(new MultiLevelListItem("List Item Four.", listItems));
this.multiLevelListInCell(workbook, multiLevelListItems, cell);
row.setHeight((short)2800);
// Insert a numbered multi-level list into cell A6. Note that the
// same ArrayList as constructed for the above plain multi-level
// list example will be re-used
row = sheet.createRow(5);
cell = row.createCell(0);
this.multiLevelNumberedListInCell(workbook, multiLevelListItems,
cell, 1, 1, 1, 2);
row.setHeight((short)2800);
// Insert a numbered multi-level list into cell A7. Note that the
// same ArrayList as constructed for the plain multi-level list
// example will be re-used
row = sheet.createRow(6);
cell = row.createCell(0);
this.multiLevelBulletedListInCell(workbook, multiLevelListItems, cell);
row.setHeight((short)2800);
// Save the completed workbook
outputFile = new File(outputFilename);
fos = new FileOutputStream(outputFile);
workbook.write(fos);
}
catch(FileNotFoundException fnfEx) {
System.out.println("Caught a: " + fnfEx.getClass().getName());
System.out.println("Message: " + fnfEx.getMessage());
System.out.println("Stacktrace follows...........");
fnfEx.printStackTrace(System.out);
}
catch(IOException ioEx) {
System.out.println("Caught a: " + ioEx.getClass().getName());
System.out.println("Message: " + ioEx.getMessage());
System.out.println("Stacktrace follows...........");
ioEx.printStackTrace(System.out);
}
finally {
if(fos != null) {
try {
fos.close();
}
catch(IOException ioEx) {
}
}
}
}
/**
* Inserts a single bulleted item into a cell.
*
* @param workbook A reference to the HSSFWorkbook that 'contains' the
* cell.
* @param listItem An instance of the String class encapsulating the
* items text.
* @param cell An instance of the HSSFCell class that encapsulates a
* reference to the spreadsheet cell into which the list item
* will be written.
*/
public void bulletedItemInCell(HSSFWorkbook workbook, String listItem, HSSFCell cell) {
// A format String must be built to ensure that the contents of the
// cell appear as a bulleted item.
HSSFDataFormat format = workbook.createDataFormat();
String formatString = InCellLists.BULLET_CHARACTER + " @";
int formatIndex = format.getFormat(formatString);
// Construct an HSSFCellStyle and set it's data formt to use the
// object created above.
HSSFCellStyle bulletStyle = workbook.createCellStyle();
bulletStyle.setDataFormat((short)formatIndex);
// Set the cells contents and style.
cell.setCellValue(new HSSFRichTextString(listItem));
cell.setCellStyle(bulletStyle);
}
/**
* Inserts a list of plain items - that is items that are neither
* numbered or bulleted - into a single cell.
*
* @param workbook A reference to the HSSFWorkbook that 'contains' the
* cell.
* @param listItems An ArrayList whose elements encapsulate the text for
* the list's items.
* @param cell An instance of the HSSFCell class that encapsulates a
* reference to the spreadsheet cell into which the list
* will be written.
*/
public void listInCell(HSSFWorkbook workbook, ArrayList<String> listItems, HSSFCell cell) {
StringBuffer buffer = new StringBuffer();
HSSFCellStyle wrapStyle = workbook.createCellStyle();
wrapStyle.setWrapText(true);
for(String listItem : listItems) {
buffer.append(listItem);
buffer.append("\n");
}
// The StringBuffer's contents are the source for the contents
// of the cell.
cell.setCellValue(new HSSFRichTextString(buffer.toString().trim()));
cell.setCellStyle(wrapStyle);
}
/**
* Inserts a numbered list into a single cell.
*
* @param workbook A reference to the HSSFWorkbook that 'contains' the
* cell.
* @param listItems An ArrayList whose elements encapsulate the text for
* the lists items.
* @param cell An instance of the HSSFCell class that encapsulates a
* reference to the spreadsheet cell into which the list
* will be written.
* @param startingValue A primitive int containing the number for the first
* item in the list.
* @param increment A primitive int containing the value that should be used
* to calculate subsequent item numbers.
*/
public void numberedListInCell(HSSFWorkbook workbook,
ArrayList<String> listItems,
HSSFCell cell,
int startingValue,
int increment) {
StringBuffer buffer = new StringBuffer();
int itemNumber = startingValue;
// Note that again, an HSSFCellStye object is required and that
// it's wrap text property should be set to 'true'
HSSFCellStyle wrapStyle = workbook.createCellStyle();
wrapStyle.setWrapText(true);
// Note that the basic method is identical to the listInCell() method
// with one difference; a number prefixed to the items text.
for(String listItem : listItems) {
buffer.append(String.valueOf(itemNumber) + ". ");
buffer.append(listItem);
buffer.append("\n");
itemNumber += increment;
}
// The StringBuffer's contents are the source for the contents
// of the cell.
cell.setCellValue(new HSSFRichTextString(buffer.toString().trim()));
cell.setCellStyle(wrapStyle);
}
/**
* Insert a bulleted list into a cell.
*
* @param workbook A reference to the HSSFWorkbook that 'contains' the
* cell.
* @param listItems An ArrayList whose elements encapsulate the text for
* the lists items.
* @param cell An instance of the HSSFCell class that encapsulates a
* reference to the spreadsheet cell into which the list
* will be written.
*/
public void bulletedListInCell(HSSFWorkbook workbook,
ArrayList<String> listItems,
HSSFCell cell) {
StringBuffer buffer = new StringBuffer();
// Note that again, an HSSFCellStye object is required and that
// it's wrap text property should be set to 'true'
HSSFCellStyle wrapStyle = workbook.createCellStyle();
wrapStyle.setWrapText(true);
// Note that the basic method is identical to the listInCell() method
// with one difference; the bullet character prefixed to the items text.
for(String listItem : listItems) {
buffer.append(InCellLists.BULLET_CHARACTER + " ");
buffer.append(listItem);
buffer.append("\n");
}
// The StringBuffer's contents are the source for the contents
// of the cell.
cell.setCellValue(new HSSFRichTextString(buffer.toString().trim()));
cell.setCellStyle(wrapStyle);
}
/**
* Insert a multi-level list into a cell.
*
* @param workbook A reference to the HSSFWorkbook that 'contains' the
* cell.
* @param multiLevelListItems An ArrayList whose elements contain instances
* of the MultiLevelListItem class. Each element
* encapsulates the text for the high level item
* along with an ArrayList. Each element of this
* ArrayList encapsulates the text for a lower
* level item.
* @param cell An instance of the HSSFCell class that encapsulates a
* reference to the spreadsheet cell into which the list
* will be written.
*/
public void multiLevelListInCell(HSSFWorkbook workbook,
ArrayList<MultiLevelListItem> multiLevelListItems,
HSSFCell cell) {
StringBuffer buffer = new StringBuffer();
ArrayList<String> lowerLevelItems = null;
// Note that again, an HSSFCellStye object is required and that
// it's wrap text property should be set to 'true'
HSSFCellStyle wrapStyle = workbook.createCellStyle();
wrapStyle.setWrapText(true);
// Step through the ArrayList of MultilLevelListItem instances.
for(MultiLevelListItem multiLevelListItem : multiLevelListItems) {
// For each element in the ArrayList, get the text for the high
// level list item......
buffer.append(multiLevelListItem.getItemText());
buffer.append("\n");
// and then an ArrayList whose elements encapsulate the text
// for the lower level list items.
lowerLevelItems = multiLevelListItem.getLowerLevelItems();
if(!(lowerLevelItems == null) && !(lowerLevelItems.isEmpty())) {
for(String item : lowerLevelItems) {
buffer.append(InCellLists.TAB);
buffer.append(item);
buffer.append("\n");
}
}
}
// The StringBuffer's contents are the source for the contents
// of the cell.
cell.setCellValue(new HSSFRichTextString(buffer.toString().trim()));
cell.setCellStyle(wrapStyle);
}
/**
* Insert a multi-level list into a cell.
*
* @param workbook A reference to the HSSFWorkbook that 'contains' the
* cell.
* @param multiLevelListItems An ArrayList whose elements contain instances
* of the MultiLevelListItem class. Each element
* encapsulates the text for the high level item
* along with an ArrayList. Each element of this
* ArrayList encapsulates the text for a lower
* level item.
* @param cell An instance of the HSSFCell class that encapsulates a
* reference to the spreadsheet cell into which the list
* will be written.
* @param highLevelStartingValue A primitive int containing the number
* for the first high level item in the list.
* @param highLevelIncrement A primitive int containing the value that
* should be used to calculate the number of
* subsequent high level item.
* @param lowLevelStartingValue A primitive int will containing the number
* for the first low level item associated
* with a high level item.
* @param lowLevelIncrement A primitive int containing the value that
* should be used to calculate the number of
* subsequent low level item.
*/
public void multiLevelNumberedListInCell(HSSFWorkbook workbook,
ArrayList<MultiLevelListItem> multiLevelListItems,
HSSFCell cell,
int highLevelStartingValue,
int highLevelIncrement,
int lowLevelStartingValue,
int lowLevelIncrement) {
StringBuffer buffer = new StringBuffer();
int highLevelItemNumber = highLevelStartingValue;
int lowLevelItemNumber = 0;
ArrayList<String> lowerLevelItems = null;
// Note that again, an HSSFCellStye object is required and that
// it's wrap text property should be set to 'true'
HSSFCellStyle wrapStyle = workbook.createCellStyle();
wrapStyle.setWrapText(true);
// Step through the ArrayList of MultilLevelListItem instances.
for(MultiLevelListItem multiLevelListItem : multiLevelListItems) {
// For each element in the ArrayList, get the text for the high
// level list item......
buffer.append(String.valueOf(highLevelItemNumber));
buffer.append(". ");
buffer.append(multiLevelListItem.getItemText());
buffer.append("\n");
// and then an ArrayList whose elements encapsulate the text
// for the lower level list items.
lowerLevelItems = multiLevelListItem.getLowerLevelItems();
if(!(lowerLevelItems == null) && !(lowerLevelItems.isEmpty())) {
lowLevelItemNumber = lowLevelStartingValue;
for(String item : lowerLevelItems) {
buffer.append(InCellLists.TAB);
buffer.append(String.valueOf(highLevelItemNumber));
buffer.append(".");
buffer.append(String.valueOf(lowLevelItemNumber));
buffer.append(" ");
buffer.append(item);
buffer.append("\n");
lowLevelItemNumber += lowLevelIncrement;
}
}
highLevelItemNumber += highLevelIncrement;
}
// The StringBuffer's contents are the source for the contents
// of the cell.
cell.setCellValue(new HSSFRichTextString(buffer.toString().trim()));
cell.setCellStyle(wrapStyle);
}
/**
* Insert a bulleted multi-level list into a cell.
*
* @param workbook A reference to the HSSFWorkbook that 'contains' the
* cell.
* @param multiLevelListItems An ArrayList whose elements contain instances
* of the MultiLevelListItem class. Each element
* encapsulates the text for the high level item
* along with an ArrayList. Each element of this
* ArrayList encapsulates the text for a lower
* level item.
* @param cell An instance of the HSSFCell class that encapsulates a
* reference to the spreadsheet cell into which the list
* will be written.
*/
public void multiLevelBulletedListInCell(HSSFWorkbook workbook,
ArrayList<MultiLevelListItem> multiLevelListItems,
HSSFCell cell) {
StringBuffer buffer = new StringBuffer();
ArrayList<String> lowerLevelItems = null;
// Note that again, an HSSFCellStye object is required and that
// it's wrap text property should be set to 'true'
HSSFCellStyle wrapStyle = workbook.createCellStyle();
wrapStyle.setWrapText(true);
// Step through the ArrayList of MultilLevelListItem instances.
for(MultiLevelListItem multiLevelListItem : multiLevelListItems) {
// For each element in the ArrayList, get the text for the high
// level list item......
buffer.append(InCellLists.BULLET_CHARACTER);
buffer.append(" ");
buffer.append(multiLevelListItem.getItemText());
buffer.append("\n");
// and then an ArrayList whose elements encapsulate the text
// for the lower level list items.
lowerLevelItems = multiLevelListItem.getLowerLevelItems();
if(!(lowerLevelItems == null) && !(lowerLevelItems.isEmpty())) {
for(String item : lowerLevelItems) {
buffer.append(InCellLists.TAB);
buffer.append(InCellLists.BULLET_CHARACTER);
buffer.append(" ");
buffer.append(item);
buffer.append("\n");
}
}
}
// The StringBuffer's contents are the source for the contents
// of the cell.
cell.setCellValue(new HSSFRichTextString(buffer.toString().trim()));
cell.setCellStyle(wrapStyle);
}
/**
* The main entry point to the program. Demonstrates how to call the method
* that will create an Excel workbook containing many different sorts of
* lists.
*
* @param args the command line arguments.
*/
public static void main(String[] args) {
new InCellLists().demonstrateMethodCalls("C:/temp/Latest In Cell List.xls");
}
/**
* An instance of this inner class models an item or element in a
* multi-level list. Each multi-level list item consists of the text for the
* high level items and an ArrayList containing the text for each of the
* associated lower level items. When written into a cell, each multi-level
* list item will have this general appearance.
*
* Item One
* Sub Item One.
* Sub Item Two.
* Item Two
* Sub Item One.
* Sub Item Two.
* etc.
*
* It would be quite possible to modify this class to model much more
* complex list structures descending through two, three or even more
* levels.
*/
public final class MultiLevelListItem {
private String itemText = null;
private ArrayList<String> lowerLevelItems = null;
/**
* Create a new instance of the MultiLevelListItem class using the
* following parameters.
*
* @param itemText A String that encapsulates the text for the high
* level list item.
* @param lowerLevelItems An ArrayList whose elements encapsulate the
* text for the associated lower level list
* items.
*/
public MultiLevelListItem(String itemText, ArrayList<String> lowerLevelItems) {
this.itemText = itemText;
this.lowerLevelItems = lowerLevelItems;
}
/**
* Get the text for the high level list item.
*
* @return A String that encapsulates the text for the high level list
* item.
*/
public String getItemText() {
return(this.itemText);
}
/**
* Get the text for the associated lower level list items.
*
* @return An ArrayList whose elements each encapsulate the text for a
* single associated lower level list item.
*/
public ArrayList<String> getLowerLevelItems() {
return(this.lowerLevelItems);
}
}
}

View File

@ -0,0 +1,47 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import java.io.IOException;
import java.io.FileOutputStream;
/**
* An example of how to merge regions of cells.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class MergedCells {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow(1);
HSSFCell cell = row.createCell(1);
cell.setCellValue("This is a test of merging");
sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,59 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Demonstrates how to use newlines in cells.
*
* @author Glen Stampoultzis (glens at apache.org)
* @author Fauzia Lala <fauzia.lala at wcom.com>
*/
public class NewLinesInCells {
public static void main( String[] args ) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
HSSFCellStyle cs = wb.createCellStyle();
HSSFFont f2 = wb.createFont();
cs = wb.createCellStyle();
cs.setFont(f2);
// Word Wrap MUST be turned on
cs.setWrapText(true);
r = s.createRow(2);
r.setHeight((short) 0x349);
c = r.createCell(2);
c.setCellType(HSSFCell.CELL_TYPE_STRING);
c.setCellValue("Use \n with word wrap on to create a new line");
c.setCellStyle(cs);
s.setColumnWidth(2, (int) ((50 * 8) / ((double) 1 / 20)));
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,43 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.util.WorkbookUtil;
import java.io.IOException;
import java.io.FileOutputStream;
/**
* Creates a new workbook with a sheet that's been explicitly defined.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class NewSheet {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFSheet sheet2 = wb.createSheet(); // create with default name
final String name = "second sheet";
wb.setSheetName(1, WorkbookUtil.createSafeSheetName(name)); // setting sheet name later
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,42 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* This example creates a new blank workbook. This workbook will contain a single blank sheet.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class NewWorkbook
{
public static void main(String[] args)
throws IOException
{
HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,321 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import java.io.*;
/**
* Demonstrates how to use the office drawing capabilities of POI.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class OfficeDrawing {
public static void main(String[] args) throws IOException {
// Create the workbook and sheets.
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFSheet sheet2 = wb.createSheet("second sheet");
HSSFSheet sheet3 = wb.createSheet("third sheet");
HSSFSheet sheet4 = wb.createSheet("fourth sheet");
HSSFSheet sheet5 = wb.createSheet("fifth sheet");
// Draw stuff in them
drawSheet1( sheet1 );
drawSheet2( sheet2 );
drawSheet3( sheet3 );
drawSheet4( sheet4, wb );
drawSheet5( sheet5, wb );
// Write the file out.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
private static void drawSheet1( HSSFSheet sheet1 )
{
// Create a row and size one of the cells reasonably large.
HSSFRow row = sheet1.createRow(2);
row.setHeight((short) 2800);
row.createCell(1);
sheet1.setColumnWidth(2, 9000);
// Create the drawing patriarch. This is the top level container for
// all shapes.
HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();
// Draw some lines and an oval.
drawLinesToCenter( patriarch );
drawManyLines( patriarch );
drawOval( patriarch );
drawPolygon( patriarch );
// Draw a rectangle.
HSSFSimpleShape rect = patriarch.createSimpleShape( new HSSFClientAnchor(100, 100, 900, 200, (short)0, 0, (short)0, 0) );
rect.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
}
private static void drawSheet2( HSSFSheet sheet2 )
{
// Create a row and size one of the cells reasonably large.
HSSFRow row = sheet2.createRow(2);
row.createCell(1);
row.setHeightInPoints(240);
sheet2.setColumnWidth(2, 9000);
// Create the drawing patriarch. This is the top level container for
// all shapes. This will clear out any existing shapes for that sheet.
HSSFPatriarch patriarch = sheet2.createDrawingPatriarch();
// Draw a grid in one of the cells.
drawGrid( patriarch );
}
private static void drawSheet3( HSSFSheet sheet3 )
{
// Create a row and size one of the cells reasonably large
HSSFRow row = sheet3.createRow(2);
row.setHeightInPoints(140);
row.createCell(1);
sheet3.setColumnWidth(2, 9000);
// Create the drawing patriarch. This is the top level container for
// all shapes. This will clear out any existing shapes for that sheet.
HSSFPatriarch patriarch = sheet3.createDrawingPatriarch();
// Create a shape group.
HSSFShapeGroup group = patriarch.createGroup(
new HSSFClientAnchor(0,0,900,200,(short)2,2,(short)2,2));
// Create a couple of lines in the group.
HSSFSimpleShape shape1 = group.createShape(new HSSFChildAnchor(3,3,500,500));
shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
( (HSSFChildAnchor) shape1.getAnchor() ).setAnchor((short)3,3,500,500);
HSSFSimpleShape shape2 = group.createShape(new HSSFChildAnchor((short)1,200,400,600));
shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
}
private static void drawSheet4( HSSFSheet sheet4, HSSFWorkbook wb )
{
// Create the drawing patriarch. This is the top level container for
// all shapes. This will clear out any existing shapes for that sheet.
HSSFPatriarch patriarch = sheet4.createDrawingPatriarch();
// Create a couple of textboxes
HSSFTextbox textbox1 = patriarch.createTextbox(
new HSSFClientAnchor(0,0,0,0,(short)1,1,(short)2,2));
textbox1.setString(new HSSFRichTextString("This is a test") );
HSSFTextbox textbox2 = patriarch.createTextbox(
new HSSFClientAnchor(0,0,900,100,(short)3,3,(short)3,4));
textbox2.setString(new HSSFRichTextString("Woo") );
textbox2.setFillColor(200,0,0);
textbox2.setLineStyle(HSSFSimpleShape.LINESTYLE_DOTGEL);
// Create third one with some fancy font styling.
HSSFTextbox textbox3 = patriarch.createTextbox(
new HSSFClientAnchor(0,0,900,100,(short)4,4,(short)5,4+1));
HSSFFont font = wb.createFont();
font.setItalic(true);
font.setUnderline(HSSFFont.U_DOUBLE);
HSSFRichTextString string = new HSSFRichTextString("Woo!!!");
string.applyFont(2,5,font);
textbox3.setString(string );
textbox3.setFillColor(0x08000030);
textbox3.setLineStyle(HSSFSimpleShape.LINESTYLE_NONE); // no line around the textbox.
textbox3.setNoFill(true); // make it transparent
}
private static void drawSheet5( HSSFSheet sheet5, HSSFWorkbook wb ) throws IOException
{
// Create the drawing patriarch. This is the top level container for
// all shapes. This will clear out any existing shapes for that sheet.
HSSFPatriarch patriarch = sheet5.createDrawingPatriarch();
HSSFClientAnchor anchor;
anchor = new HSSFClientAnchor(0,0,0,255,(short)2,2,(short)4,7);
anchor.setAnchorType( 2 );
patriarch.createPicture(anchor, loadPicture( "src/resources/logos/logoKarmokar4.png", wb ));
anchor = new HSSFClientAnchor(0,0,0,255,(short)4,2,(short)5,7);
anchor.setAnchorType( 2 );
patriarch.createPicture(anchor, loadPicture( "src/resources/logos/logoKarmokar4edited.png", wb ));
anchor = new HSSFClientAnchor(0,0,1023,255,(short)6,2,(short)8,7);
anchor.setAnchorType( 2 );
HSSFPicture picture = patriarch.createPicture(anchor, loadPicture( "src/resources/logos/logoKarmokar4s.png", wb ));
//Reset the image to the original size.
picture.resize();
picture.setLineStyle( picture.LINESTYLE_DASHDOTGEL );
}
private static int loadPicture( String path, HSSFWorkbook wb ) throws IOException
{
int pictureIndex;
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
try
{
fis = new FileInputStream( path);
bos = new ByteArrayOutputStream( );
int c;
while ( (c = fis.read()) != -1)
bos.write( c );
pictureIndex = wb.addPicture( bos.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG );
}
finally
{
if (fis != null)
fis.close();
if (bos != null)
bos.close();
}
return pictureIndex;
}
private static void drawOval( HSSFPatriarch patriarch )
{
// Create an oval and style to taste.
HSSFClientAnchor a = new HSSFClientAnchor();
a.setAnchor((short)2, 2, 20, 20, (short) 2, 2, 190, 80);
HSSFSimpleShape s = patriarch.createSimpleShape(a);
s.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
s.setLineStyleColor(10,10,10);
s.setFillColor(90,10,200);
s.setLineWidth(HSSFShape.LINEWIDTH_ONE_PT * 3);
s.setLineStyle(HSSFShape.LINESTYLE_DOTSYS);
}
private static void drawPolygon( HSSFPatriarch patriarch )
{
// HSSFClientAnchor a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 2, 2, (short) 3, 3 );
// HSSFPolygon p = patriarch.createPolygon(a);
// p.setPolygonDrawArea(100,100);
// p.setPoints( new int[]{30, 90, 50}, new int[]{88, 5, 44} );
HSSFClientAnchor a = new HSSFClientAnchor();
a.setAnchor( (short) 2, 2, 0, 0, (short) 3, 3, 1023, 255 );
HSSFShapeGroup g = patriarch.createGroup( a );
g.setCoordinates(0,0,200,200);
HSSFPolygon p1 = g.createPolygon( new HSSFChildAnchor( 0, 0, 200, 200 ) );
p1.setPolygonDrawArea( 100, 100 );
p1.setPoints( new int[]{0, 90, 50}, new int[]{5, 5, 44} );
p1.setFillColor( 0, 255, 0 );
HSSFPolygon p2 = g.createPolygon( new HSSFChildAnchor( 20, 20, 200, 200 ) );
p2.setPolygonDrawArea( 200, 200 );
p2.setPoints( new int[]{120, 20, 150}, new int[]{105, 30, 195} );
p2.setFillColor( 255, 0, 0 );
}
private static void drawManyLines( HSSFPatriarch patriarch )
{
// Draw bunch of lines
int x1 = 100;
int y1 = 100;
int x2 = 800;
int y2 = 200;
int color = 0;
for (int i = 0; i < 10; i++)
{
HSSFClientAnchor a2 = new HSSFClientAnchor();
a2.setAnchor((short) 2, 2, x1, y1, (short) 2, 2, x2, y2);
HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2);
shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
shape2.setLineStyleColor(color);
y1 -= 10;
y2 -= 10;
color += 30;
}
}
private static void drawGrid( HSSFPatriarch patriarch )
{
// This draws a grid of lines. Since the coordinates space fixed at
// 1024 by 256 we use a ratio to get a reasonably square grids.
double xRatio = 3.22;
double yRatio = 0.6711;
int x1 = 000;
int y1 = 000;
int x2 = 000;
int y2 = 200;
for (int i = 0; i < 20; i++)
{
HSSFClientAnchor a2 = new HSSFClientAnchor();
a2.setAnchor((short) 2, 2, (int) ( x1 * xRatio ), (int) ( y1 * yRatio ),
(short) 2, 2, (int) ( x2 * xRatio ), (int) ( y2 * yRatio ));
HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2);
shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
x1 += 10;
x2 += 10;
}
x1 = 000;
y1 = 000;
x2 = 200;
y2 = 000;
for (int i = 0; i < 20; i++)
{
HSSFClientAnchor a2 = new HSSFClientAnchor();
a2.setAnchor((short) 2, 2, (int) ( x1 * xRatio ), (int) ( y1 * yRatio ),
(short) 2, 2, (int) ( x2 * xRatio ), (int) ( y2 * yRatio ));
HSSFSimpleShape shape2 = patriarch.createSimpleShape(a2);
shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
y1 += 10;
y2 += 10;
}
}
private static void drawLinesToCenter( HSSFPatriarch patriarch )
{
// Draw some lines from and to the corners
{
HSSFClientAnchor a1 = new HSSFClientAnchor();
a1.setAnchor( (short)2, 2, 0, 0, (short) 2, 2, 512, 128);
HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
}
{
HSSFClientAnchor a1 = new HSSFClientAnchor();
a1.setAnchor( (short)2, 2, 512, 128, (short) 2, 2, 1024, 0);
HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
}
{
HSSFClientAnchor a1 = new HSSFClientAnchor();
a1.setAnchor( (short)1, 1, 0, 0, (short) 1, 1, 512, 100);
HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
}
{
HSSFClientAnchor a1 = new HSSFClientAnchor();
a1.setAnchor( (short)1, 1, 512, 100, (short) 1, 1, 1024, 0);
HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
}
}
}

View File

@ -0,0 +1,103 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Demonstrates the use of the EscherGraphics2d library.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class OfficeDrawingWithGraphics {
public static void main( String[] args ) throws IOException {
// Create a workbook with one sheet and size the first three somewhat
// larger so we can fit the chemical structure diagram in.
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet( "my drawing" );
sheet.setColumnWidth(1, 256 * 27);
HSSFRow row1 = sheet.createRow(0);
row1.setHeightInPoints(10 * 15);
HSSFRow row2 = sheet.createRow(1);
row2.setHeightInPoints(5 * 15);
HSSFRow row3 = sheet.createRow(2);
row3.setHeightInPoints(10 * 15);
// Add some cells so we can test that the anchoring works when we
// sort them.
row1.createCell(0).setCellValue("C");
row2.createCell(0).setCellValue("A");
row3.createCell(0).setCellValue("B");
// Create the top level drawing patriarch.
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor a;
HSSFShapeGroup group;
EscherGraphics g;
EscherGraphics2d g2d;
// Anchor entirely within one cell.
a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 0, (short) 1, 0 );
group = patriarch.createGroup( a );
group.setCoordinates( 0, 0, 320, 276 );
float verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / Math.abs(group.getY2() - group.getY1());
g = new EscherGraphics( group, wb, Color.black, verticalPointsPerPixel );
g2d = new EscherGraphics2d( g );
drawStar( g2d );
a = new HSSFClientAnchor( 0, 0, 1023, 255, (short) 1, 1, (short) 1, 1 );
group = patriarch.createGroup( a );
group.setCoordinates( 0, 0, 640, 276 );
verticalPointsPerPixel = a.getAnchorHeightInPoints(sheet) / Math.abs(group.getY2() - group.getY1());
// verticalPixelsPerPoint = (float)Math.abs(group.getY2() - group.getY1()) / a.getAnchorHeightInPoints(sheet);
g = new EscherGraphics( group, wb, Color.black, verticalPointsPerPixel );
g2d = new EscherGraphics2d( g );
drawStar( g2d );
FileOutputStream out = new FileOutputStream("workbook.xls");
wb.write(out);
out.close();
}
private static void drawStar( EscherGraphics2d g2d )
{
g2d.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON );
for (double i = 0; i < Math.PI; i += 0.1)
{
g2d.setColor( new Color((int)(i * 5343062d) ) );
int x1 = (int) ( Math.cos(i) * 160.0 ) + 160;
int y1 = (int) ( Math.sin(i) * 138.0 ) + 138;
int x2 = (int) ( -Math.cos(i) * 160.0 ) + 160;
int y2 = (int) ( -Math.sin(i) * 138.0 ) + 138;
g2d.setStroke(new BasicStroke(2));
g2d.drawLine(x1,y1,x2,y2);
}
g2d.setFont(new Font("SansSerif",Font.BOLD | Font.ITALIC, 20));
g2d.drawString("EscherGraphics2d",70,100);
g2d.setColor(Color.yellow);
g2d.fillOval( 160-20,138-20,40,40);
g2d.setColor(Color.black);
g2d.fillPolygon(new int[] {-10+160,0+160,10+160,0+160}, new int[] {0+138,10+138,0+138,-10+138}, 4);
g2d.drawPolygon(new int[] {-160+160,0+160,160+160,0+160}, new int[] {0+138,138+138,0+138,-138+138}, 4);
}
}

View File

@ -0,0 +1,259 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Creates outlines.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class Outlines {
public static void main(String[] args) throws IOException {
createCase1( "outline1.xls" );
System.out.println( "outline1.xls written. Two expanded groups." );
createCase2( "outline2.xls" );
System.out.println( "outline2.xls written. Two groups. Inner group collapsed." );
createCase3( "outline3.xls" );
System.out.println( "outline3.xls written. Two groups. Both collapsed." );
createCase4( "outline4.xls" );
System.out.println( "outline4.xls written. Two groups. Collapsed then inner group expanded." );
createCase5( "outline5.xls" );
System.out.println( "outline5.xls written. Two groups. Collapsed then reexpanded." );
createCase6( "outline6.xls" );
System.out.println( "outline6.xls written. Two groups with matching end points. Second group collapsed." );
createCase7( "outline7.xls" );
System.out.println( "outline7.xls written. Row outlines." );
createCase8( "outline8.xls" );
System.out.println( "outline8.xls written. Row outlines. Inner group collapsed." );
createCase9( "outline9.xls" );
System.out.println( "outline9.xls written. Row outlines. Both collapsed." );
createCase10( "outline10.xls" );
System.out.println( "outline10.xls written. Row outlines. Collapsed then inner group expanded." );
createCase11( "outline11.xls" );
System.out.println( "outline11.xls written. Row outlines. Collapsed then expanded." );
createCase12( "outline12.xls" );
System.out.println( "outline12.xls written. Row outlines. Two row groups with matching end points. Second group collapsed." );
createCase13( "outline13.xls" );
System.out.println( "outline13.xls written. Mixed bag." );
}
private static void createCase1(String filename) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupColumn(4, 7);
for (int row = 0; row < 200; row++) {
HSSFRow r = sheet1.createRow(row);
for (int column = 0; column < 200; column++) {
HSSFCell c = r.createCell(column);
c.setCellValue(column);
}
}
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
}
private static void createCase2(String filename) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupColumn(2, 10);
sheet1.groupColumn(4, 7);
sheet1.setColumnGroupCollapsed(4, true);
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
}
private static void createCase3(String filename) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupColumn(2, 10);
sheet1.groupColumn(4, 7);
sheet1.setColumnGroupCollapsed(4, true);
sheet1.setColumnGroupCollapsed(2, true);
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
}
private static void createCase4(String filename) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupColumn(2, 10);
sheet1.groupColumn(4, 7);
sheet1.setColumnGroupCollapsed(4, true);
sheet1.setColumnGroupCollapsed(2, true);
sheet1.setColumnGroupCollapsed(4, false);
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
}
private static void createCase5(String filename) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupColumn(2, 10);
sheet1.groupColumn(4, 7);
sheet1.setColumnGroupCollapsed(4, true);
sheet1.setColumnGroupCollapsed(2, true);
sheet1.setColumnGroupCollapsed(4, false);
sheet1.setColumnGroupCollapsed(3, false);
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
}
private static void createCase6(String filename) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupColumn(2, 10);
sheet1.groupColumn(4, 10);
sheet1.setColumnGroupCollapsed(4, true);
sheet1.setColumnGroupCollapsed(2, true);
sheet1.setColumnGroupCollapsed(3, false);
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
}
private static void createCase7(String filename) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow(5, 14);
sheet1.groupRow(7, 10);
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
}
private static void createCase8(String filename) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow(5, 14);
sheet1.groupRow(7, 10);
sheet1.setRowGroupCollapsed(7, true);
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
}
private static void createCase9(String filename) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow(5, 14);
sheet1.groupRow(7, 10);
sheet1.setRowGroupCollapsed(7, true);
sheet1.setRowGroupCollapsed(5, true);
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
}
private static void createCase10(String filename) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow(5, 14);
sheet1.groupRow(7, 10);
sheet1.setRowGroupCollapsed(7, true);
sheet1.setRowGroupCollapsed(5, true);
sheet1.setRowGroupCollapsed(8, false);
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
}
private static void createCase11(String filename) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow(5, 14);
sheet1.groupRow(7, 10);
sheet1.setRowGroupCollapsed(7, true);
sheet1.setRowGroupCollapsed(5, true);
sheet1.setRowGroupCollapsed(8, false);
sheet1.setRowGroupCollapsed(14, false);
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
}
private static void createCase12(String filename) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow(5, 14);
sheet1.groupRow(7, 14);
sheet1.setRowGroupCollapsed(7, true);
sheet1.setRowGroupCollapsed(5, true);
sheet1.setRowGroupCollapsed(6, false);
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
}
private static void createCase13(String filename) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.groupRow(5, 14);
sheet1.groupRow(7, 14);
sheet1.groupRow(16, 19);
sheet1.groupColumn(4, 7);
sheet1.groupColumn(9, 12);
sheet1.groupColumn(10, 11);
FileOutputStream fileOut = new FileOutputStream(filename);
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,66 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* This example demonstrates opening a workbook, modifying it and writing
* the results back out.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class ReadWriteWorkbook {
public static void main(String[] args) throws IOException {
FileInputStream fileIn = null;
FileOutputStream fileOut = null;
try
{
fileIn = new FileInputStream("workbook.xls");
POIFSFileSystem fs = new POIFSFileSystem(fileIn);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(2);
if (row == null)
row = sheet.createRow(2);
HSSFCell cell = row.getCell(3);
if (cell == null)
cell = row.createCell(3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
fileOut = new FileOutputStream("workbookout.xls");
wb.write(fileOut);
} finally {
if (fileOut != null)
fileOut.close();
if (fileIn != null)
fileIn.close();
}
}
}

View File

@ -0,0 +1,58 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import java.io.IOException;
import java.io.FileOutputStream;
/**
* @author Glen Stampoultzis (glens at apache.org)
*/
public class RepeatingRowsAndColumns {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("first sheet");
wb.createSheet("second sheet");
wb.createSheet("third sheet");
HSSFFont boldFont = wb.createFont();
boldFont.setFontHeightInPoints((short)22);
boldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle boldStyle = wb.createCellStyle();
boldStyle.setFont(boldFont);
HSSFRow row = sheet1.createRow(1);
HSSFCell cell = row.createCell(0);
cell.setCellValue("This quick brown fox");
cell.setCellStyle(boldStyle);
// Set the columns to repeat from column 0 to 2 on the first sheet
wb.setRepeatingRowsAndColumns(0,0,2,-1,-1);
// Set the rows to repeat from row 0 to 2 on the second sheet.
wb.setRepeatingRowsAndColumns(1,-1,-1,0,2);
// Set the the repeating rows and columns on the third sheet.
wb.setRepeatingRowsAndColumns(2,4,5,1,2);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,55 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import java.io.IOException;
import java.io.FileOutputStream;
/**
* @author Glen Stampoultzis (glens at apache.org)
*/
public class SplitAndFreezePanes
{
public static void main(String[] args)
throws IOException
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFSheet sheet2 = wb.createSheet("second sheet");
HSSFSheet sheet3 = wb.createSheet("third sheet");
HSSFSheet sheet4 = wb.createSheet("fourth sheet");
// Freeze just one row
sheet1.createFreezePane( 0, 1, 0, 1 );
// Freeze just one column
sheet2.createFreezePane( 1, 0, 1, 0 );
// Freeze the columns and rows (forget about scrolling position of the lower right quadrant).
sheet3.createFreezePane( 2, 2 );
// Create a split with the lower left side being the active quadrant
sheet4.createSplitPane( 2000, 2000, 0, 0, HSSFSheet.PANE_LOWER_LEFT );
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,59 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Demonstrates how to create and use fonts.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class WorkingWithFonts {
public static void main(String[] args) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow(1);
// Create a new font and alter it.
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)24);
font.setFontName("Courier New");
font.setItalic(true);
font.setStrikeout(true);
// Fonts are set into a style so create a new one to use.
HSSFCellStyle style = wb.createCellStyle();
style.setFont(font);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell(1);
cell.setCellValue("This is a test of fonts");
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,45 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import java.io.IOException;
import java.io.FileOutputStream;
/**
* Sets the zoom magnication for a sheet.
*
* @author Glen Stampoultzis (glens at apache.org)
*/
public class ZoomSheet
{
public static void main(String[] args)
throws IOException
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.setZoom(3,4); // 75 percent magnification
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}

View File

@ -0,0 +1,564 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.view;
import java.awt.*;
import javax.swing.border.AbstractBorder;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
/**
* This is an attempt to implement Excel style borders for the SheetViewer.
* Mostly just overrides stuff so the javadoc won't appear here but will
* appear in the generated stuff.
*
* @author Andrew C. Oliver (acoliver at apache dot org)
* @author Jason Height
*/
public class SVBorder extends AbstractBorder {
private Color northColor = null;
private Color eastColor = null;
private Color southColor = null;
private Color westColor = null;
private int northBorderType = HSSFCellStyle.BORDER_NONE;
private int eastBorderType =HSSFCellStyle.BORDER_NONE;
private int southBorderType = HSSFCellStyle.BORDER_NONE;
private int westBorderType = HSSFCellStyle.BORDER_NONE;
private boolean northBorder=false;
private boolean eastBorder=false;
private boolean southBorder=false;
private boolean westBorder=false;
private boolean selected = false;
public void setBorder(Color northColor, Color eastColor,
Color southColor, Color westColor,
int northBorderType, int eastBorderType,
int southBorderType, int westBorderType,
boolean selected) {
this.eastColor = eastColor;
this.southColor = southColor;
this.westColor = westColor;
this.northBorderType = northBorderType;
this.eastBorderType = eastBorderType;
this.southBorderType = southBorderType;
this.westBorderType = westBorderType;
this.northBorder=northBorderType != HSSFCellStyle.BORDER_NONE;
this.eastBorder=eastBorderType != HSSFCellStyle.BORDER_NONE;
this.southBorder=southBorderType != HSSFCellStyle.BORDER_NONE;
this.westBorder=westBorderType != HSSFCellStyle.BORDER_NONE;
this.selected = selected;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
Color oldColor = g.getColor();
paintSelectedBorder(g, x, y, width, height);
paintNormalBorders(g, x, y, width, height);
paintDottedBorders(g, x, y, width, height);
paintDashedBorders(g, x, y, width, height);
paintDoubleBorders(g, x, y, width, height);
paintDashDotDotBorders(g, x, y, width, height);
g.setColor(oldColor);
}
/**
* Called by paintBorder to paint the border of a selected cell.
* The paramaters are the Graphics object, location and dimensions of the
* cell.
*/
private void paintSelectedBorder(Graphics g, int x, int y, int width,
int height) {
if (selected) {
//Need to setup thickness of 2
g.setColor(Color.black);
//paint the border
g.drawRect(x,y,width-1,height-1);
//paint the filled rectangle at the bottom left hand position
g.fillRect(x+width-5, y+height-5, 5, 5);
}
}
/**
* Called by paintBorder to paint the various versions of normal line
* borders for a cell.
*/
private void paintNormalBorders(Graphics g, int x, int y, int width,
int height) {
if (northBorder &&
((northBorderType == HSSFCellStyle.BORDER_THIN) ||
(northBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
(northBorderType == HSSFCellStyle.BORDER_THICK)
)
) {
int thickness = getThickness(northBorderType);
g.setColor(northColor);
for (int k=0; k < thickness; k++) {
g.drawLine(x,y+k,width,y+k);
}
}
if (eastBorder &&
((eastBorderType == HSSFCellStyle.BORDER_THIN) ||
(eastBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
(eastBorderType == HSSFCellStyle.BORDER_THICK)
)
) {
int thickness = getThickness(eastBorderType);
g.setColor(eastColor);
for (int k=0; k < thickness; k++) {
g.drawLine(width-k,y,width-k,height);
}
}
if (southBorder &&
((southBorderType == HSSFCellStyle.BORDER_THIN) ||
(southBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
(southBorderType == HSSFCellStyle.BORDER_THICK)
)
) {
int thickness = getThickness(southBorderType);
g.setColor(southColor);
for (int k=0; k < thickness; k++) {
g.drawLine(x,height - k,width,height - k);
}
}
if (westBorder &&
((westBorderType == HSSFCellStyle.BORDER_THIN) ||
(westBorderType == HSSFCellStyle.BORDER_MEDIUM) ||
(westBorderType == HSSFCellStyle.BORDER_THICK)
)
) {
int thickness = getThickness(westBorderType);
g.setColor(westColor);
for (int k=0; k < thickness; k++) {
g.drawLine(x+k,y,x+k,height);
}
}
}
/**
* Called by paintBorder to paint the dotted line
* borders for a cell.
*/
private void paintDottedBorders(Graphics g, int x, int y, int width,
int height) {
if (northBorder &&
northBorderType == HSSFCellStyle.BORDER_DOTTED) {
int thickness = getThickness(northBorderType);
g.setColor(northColor);
for (int k=0; k < thickness; k++) {
for (int xc = x; xc < width; xc=xc+2) {
g.drawLine(xc,y+k,xc,y+k);
}
}
}
if (eastBorder &&
eastBorderType == HSSFCellStyle.BORDER_DOTTED
) {
int thickness = getThickness(eastBorderType);
thickness++; //need for dotted borders to show up east
g.setColor(eastColor);
for (int k=0; k < thickness; k++) {
for (int yc=y;yc < height; yc=yc+2) {
g.drawLine(width-k,yc,width-k,yc);
}
}
}
if (southBorder &&
southBorderType == HSSFCellStyle.BORDER_DOTTED
) {
int thickness = getThickness(southBorderType);
thickness++;
g.setColor(southColor);
for (int k=0; k < thickness; k++) {
for (int xc = x; xc < width; xc=xc+2) {
g.drawLine(xc,height-k,xc,height-k);
}
}
}
if (westBorder &&
westBorderType == HSSFCellStyle.BORDER_DOTTED
) {
int thickness = getThickness(westBorderType);
// thickness++;
g.setColor(westColor);
for (int k=0; k < thickness; k++) {
for (int yc=y;yc < height; yc=yc+2) {
g.drawLine(x+k,yc,x+k,yc);
}
}
}
}
/**
* Called by paintBorder to paint the various versions of dotted line
* borders for a cell.
*/
private void paintDashedBorders(Graphics g, int x, int y, int width,
int height) {
if (northBorder &&
((northBorderType == HSSFCellStyle.BORDER_DASHED) ||
(northBorderType == HSSFCellStyle.BORDER_HAIR))
) {
int thickness = getThickness(northBorderType);
int dashlength = 1;
if (northBorderType == HSSFCellStyle.BORDER_DASHED)
dashlength = 2;
g.setColor(northColor);
for (int k=0; k < thickness; k++) {
for (int xc = x; xc < width; xc=xc+5) {
g.drawLine(xc,y+k,xc+dashlength,y+k);
}
}
}
if (eastBorder &&
((eastBorderType == HSSFCellStyle.BORDER_DASHED) ||
(eastBorderType == HSSFCellStyle.BORDER_HAIR))
) {
int thickness = getThickness(eastBorderType);
thickness++; //need for dotted borders to show up east
int dashlength = 1;
if (eastBorderType == HSSFCellStyle.BORDER_DASHED)
dashlength = 2;
g.setColor(eastColor);
for (int k=0; k < thickness; k++) {
for (int yc=y;yc < height; yc=yc+5) {
g.drawLine(width-k,yc,width-k,yc+dashlength);
}
}
}
if (southBorder &&
((southBorderType == HSSFCellStyle.BORDER_DASHED) ||
(southBorderType == HSSFCellStyle.BORDER_HAIR))
) {
int thickness = getThickness(southBorderType);
thickness++;
int dashlength = 1;
if (southBorderType == HSSFCellStyle.BORDER_DASHED)
dashlength = 2;
g.setColor(southColor);
for (int k=0; k < thickness; k++) {
for (int xc = x; xc < width; xc=xc+5) {
g.drawLine(xc,height-k,xc+dashlength,height-k);
}
}
}
if (westBorder &&
((westBorderType == HSSFCellStyle.BORDER_DASHED) ||
(westBorderType == HSSFCellStyle.BORDER_HAIR))
) {
int thickness = getThickness(westBorderType);
// thickness++;
int dashlength = 1;
if (westBorderType == HSSFCellStyle.BORDER_DASHED)
dashlength = 2;
g.setColor(westColor);
for (int k=0; k < thickness; k++) {
for (int yc=y;yc < height; yc=yc+5) {
g.drawLine(x+k,yc,x+k,yc+dashlength);
}
}
}
}
/**
* Called by paintBorder to paint the double line
* borders for a cell.
*/
private void paintDoubleBorders(Graphics g, int x, int y, int width,
int height) {
if (northBorder &&
northBorderType == HSSFCellStyle.BORDER_DOUBLE) {
g.setColor(northColor);
int leftx=x;
int rightx=width;
// if there are borders on the west or east then
// the second line shouldn't cross them
if (westBorder)
leftx = x+3;
if (eastBorder)
rightx = width-3;
g.drawLine(x,y,width,y);
g.drawLine(leftx,y+2,rightx,y+2);
}
if (eastBorder &&
eastBorderType == HSSFCellStyle.BORDER_DOUBLE
) {
int thickness = getThickness(eastBorderType);
thickness++; //need for dotted borders to show up east
g.setColor(eastColor);
int topy=y;
int bottomy=height;
if (northBorder)
topy=y+3;
if (southBorder)
bottomy=height-3;
g.drawLine(width-1,y,width-1,height);
g.drawLine(width-3,topy,width-3,bottomy);
}
if (southBorder &&
southBorderType == HSSFCellStyle.BORDER_DOUBLE
) {
g.setColor(southColor);
int leftx=y;
int rightx=width;
if (westBorder)
leftx=x+3;
if (eastBorder)
rightx=width-3;
g.drawLine(x,height - 1,width,height - 1);
g.drawLine(leftx,height - 3,rightx,height - 3);
}
if (westBorder &&
westBorderType == HSSFCellStyle.BORDER_DOUBLE
) {
int thickness = getThickness(westBorderType);
// thickness++;
g.setColor(westColor);
int topy=y;
int bottomy=height-3;
if (northBorder)
topy=y+2;
if (southBorder)
bottomy=height-3;
g.drawLine(x,y,x,height);
g.drawLine(x+2,topy,x+2,bottomy);
}
}
/**
* Called by paintBorder to paint the various versions of dash dot dot line
* borders for a cell.
*/
private void paintDashDotDotBorders(Graphics g, int x, int y, int width,
int height) {
if (northBorder &&
((northBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
(northBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
) {
int thickness = getThickness(northBorderType);
g.setColor(northColor);
for (int l=x; l < width;) {
l=l+drawDashDotDot(g, l, y, thickness, true, true);
}
}
if (eastBorder &&
((eastBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
(eastBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
) {
int thickness = getThickness(eastBorderType);
g.setColor(eastColor);
for (int l=y;l < height;) {
//System.err.println("drawing east");
l=l+drawDashDotDot(g,width-1,l,thickness,false,false);
}
}
if (southBorder &&
((southBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
(southBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
) {
int thickness = getThickness(southBorderType);
g.setColor(southColor);
for (int l=x; l < width;) {
//System.err.println("drawing south");
l=l+drawDashDotDot(g, l, height-1, thickness, true, false);
}
}
if (westBorder &&
((westBorderType == HSSFCellStyle.BORDER_DASH_DOT_DOT) ||
(westBorderType == HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT))
) {
int thickness = getThickness(westBorderType);
g.setColor(westColor);
for (int l=y;l < height;) {
//System.err.println("drawing west");
l=l+drawDashDotDot(g,x,l,thickness,false,true);
}
}
}
/**
* Draws one dash dot dot horizontally or vertically with thickness drawn
* incrementally to either the right or left.
*
* @param g graphics object for drawing with
* @param x the x origin of the line
* @param y the y origin of the line
* @param thickness the thickness of the line
* @param horizontal or vertical (true for horizontal)
* @param right/bottom or left/top thickness (true for right or top),
* if true then the x or y origin will be incremented to provide
* thickness, if false, they'll be decremented. For vertical
* borders, x is incremented or decremented, for horizontal its y.
* Just set to true for north and west, and false for east and
* south.
* @returns length - returns the length of the line.
*/
private int drawDashDotDot(Graphics g,int x, int y, int thickness,
boolean horizontal,
boolean rightBottom) {
for (int t=0; t < thickness; t++) {
if (!rightBottom) {
t = 0 - t; //add negative thickness so we go the other way
//then we'll decrement instead of increment.
}
if (horizontal) {
g.drawLine(x,y+t,x+5,y+t);
g.drawLine(x+8,y+t,x+10,y+t);
g.drawLine(x+13,y+t,x+15,y+t);
} else {
g.drawLine(x+t,y,x+t,y+5);
g.drawLine(x+t,y+8,x+t,y+10);
g.drawLine(x+t,y+13,x+t,y+15);
}
}
return 18;
}
/**
* @returns the line thickness for a border based on border type
*/
private int getThickness(int thickness) {
int retval=1;
switch (thickness) {
case HSSFCellStyle.BORDER_THIN:
retval=2;
break;
case HSSFCellStyle.BORDER_MEDIUM:
retval=3;
break;
case HSSFCellStyle.BORDER_THICK:
retval=4;
break;
case HSSFCellStyle.BORDER_DASHED:
retval=1;
break;
case HSSFCellStyle.BORDER_DASH_DOT_DOT:
retval=1;
break;
case HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT:
retval=3;
break;
case HSSFCellStyle.BORDER_HAIR:
retval=1;
break;
default:
retval=1;
}
return retval;
}
}

View File

@ -0,0 +1,220 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.view;
import java.text.*;
/**
* This class is used to format cells into their fractional format.
*
* I cant be 100% sure that the same fractional value will be displayed as in
* excel but then again it is a lossy formating mode anyway
*
* @author Jason Height
* @since 15 July 2002
*/
public class SVFractionalFormat extends Format {
private short ONE_DIGIT = 1;
private short TWO_DIGIT = 2;
private short THREE_DIGIT = 3;
private short UNITS = 4;
private int units = 1;
private short mode = -1;
/** Constructs a new FractionalFormatter
*
* The formatStr defines how the number will be formatted
* # ?/? Up to one digit
* # ??/?? Up to two digits
* # ???/??? Up to three digits
* # ?/2 In halves
* # ?/4 In quarters
* # ?/8 In eighths
* # ?/16 In sixteenths
* # ?/10 In tenths
* # ?/100 In hundredths
*/
public SVFractionalFormat(String formatStr) {
if ("# ?/?".equals(formatStr))
mode = ONE_DIGIT;
else if ("# ??/??".equals(formatStr))
mode = TWO_DIGIT;
else if ("# ???/???".equals(formatStr))
mode = THREE_DIGIT;
else if ("# ?/2".equals(formatStr)) {
mode = UNITS;
units = 2;
} else if ("# ?/4".equals(formatStr)) {
mode = UNITS;
units = 4;
} else if ("# ?/8".equals(formatStr)) {
mode = UNITS;
units = 8;
} else if ("# ?/16".equals(formatStr)) {
mode = UNITS;
units = 16;
} else if ("# ?/10".equals(formatStr)) {
mode = UNITS;
units = 10;
} else if ("# ?/100".equals(formatStr)) {
mode = UNITS;
units = 100;
}
}
/**
* Returns a fractional string representation of a double to a maximum denominator size
*
* This code has been translated to java from the following web page.
* http://www.codeproject.com/cpp/fraction.asp
* Originally coded in c++ By Dean Wyant dwyant@mindspring.com
* The code on the web page is freely available.
*
* @param f Description of the Parameter
* @param MaxDen Description of the Parameter
* @return Description of the Return Value
*/
private String format(final double f, final int MaxDen) {
long Whole = (long)f;
int sign = 1;
if (f < 0) {
sign = -1;
}
double Precision = 0.00001;
double AllowedError = Precision;
double d = Math.abs(f);
d -= Whole;
double Frac = d;
double Diff = Frac;
long Num = 1;
long Den = 0;
long A = 0;
long B = 0;
long i = 0;
if (Frac > Precision) {
while (true) {
d = 1.0 / d;
i = (long) (d + Precision);
d -= i;
if (A > 0) {
Num = i * Num + B;
}
Den = (long) (Num / Frac + 0.5);
Diff = Math.abs((double) Num / Den - Frac);
if (Den > MaxDen) {
if (A > 0) {
Num = A;
Den = (long) (Num / Frac + 0.5);
Diff = Math.abs((double) Num / Den - Frac);
} else {
Den = MaxDen;
Num = 1;
Diff = Math.abs((double) Num / Den - Frac);
if (Diff > Frac) {
Num = 0;
Den = 1;
// Keeps final check below from adding 1 and keeps Den from being 0
Diff = Frac;
}
}
break;
}
if ((Diff <= AllowedError) || (d < Precision)) {
break;
}
Precision = AllowedError / Diff;
// This calcualtion of Precision does not always provide results within
// Allowed Error. It compensates for loss of significant digits that occurs.
// It helps to round the inprecise reciprocal values to i.
B = A;
A = Num;
}
}
if (Num == Den) {
Whole++;
Num = 0;
Den = 0;
} else if (Den == 0) {
Num = 0;
}
if (sign < 0) {
if (Whole == 0) {
Num = -Num;
} else {
Whole = -Whole;
}
}
return new StringBuffer().append(Whole).append(" ").append(Num).append("/").append(Den).toString();
}
/** This method formats the double in the units specified.
* The usints could be any number but in this current implementation it is
* halves (2), quaters (4), eigths (8) etc
*/
private String formatUnit(double f, int units) {
long Whole = (long)f;
f -= Whole;
long Num = Math.round(f * units);
return new StringBuffer().append(Whole).append(" ").append(Num).append("/").append(units).toString();
}
public final String format(double val) {
if (mode == ONE_DIGIT) {
return format(val, 9);
} else if (mode == TWO_DIGIT) {
return format(val, 99);
} else if (mode == THREE_DIGIT) {
return format(val, 999);
} else if (mode == UNITS) {
return formatUnit(val , units);
}
throw new RuntimeException("Unexpected Case");
}
public StringBuffer format(Object obj,
StringBuffer toAppendTo,
FieldPosition pos) {
if (obj instanceof Number) {
toAppendTo.append(format(((Number)obj).doubleValue()));
return toAppendTo;
}
throw new IllegalArgumentException("Can only handle Numbers");
}
public Object parseObject(String source,
ParsePosition status) {
//JMH TBD
return null;
}
public Object parseObject(String source)
throws ParseException {
//JMH TBD
return null;
}
public Object clone() {
//JMH TBD
return null;
}
}

View File

@ -0,0 +1,96 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.view;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import org.apache.poi.hssf.usermodel.*;
/**
* This class presents the row header to the table.
*
*
* @author Jason Height
*/
public class SVRowHeader extends JList {
/** This model simply returns an integer number up to the number of rows
* that are present in the sheet.
*
*/
private class SVRowHeaderModel extends AbstractListModel {
private HSSFSheet sheet;
public SVRowHeaderModel(HSSFSheet sheet) {
this.sheet = sheet;
}
public int getSize() {
return sheet.getLastRowNum() + 1;
}
public Object getElementAt(int index) {
return Integer.toString(index+1);
}
}
/** Renderes the row number*/
private class RowHeaderRenderer extends JLabel implements ListCellRenderer {
private HSSFSheet sheet;
private int extraHeight;
RowHeaderRenderer(HSSFSheet sheet, JTable table, int extraHeight) {
this.sheet = sheet;
this.extraHeight = extraHeight;
JTableHeader header = table.getTableHeader();
setOpaque(true);
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setHorizontalAlignment(CENTER);
setForeground(header.getForeground());
setBackground(header.getBackground());
setFont(header.getFont());
}
public Component getListCellRendererComponent( JList list,
Object value, int index, boolean isSelected, boolean cellHasFocus) {
Dimension d = getPreferredSize();
HSSFRow row = sheet.getRow(index);
int rowHeight;
if(row == null) {
rowHeight = (int)sheet.getDefaultRowHeightInPoints();
} else {
rowHeight = (int)row.getHeightInPoints();
}
d.height = rowHeight+extraHeight;
setPreferredSize(d);
setText((value == null) ? "" : value.toString());
return this;
}
}
public SVRowHeader(HSSFSheet sheet, JTable table, int extraHeight) {
ListModel lm = new SVRowHeaderModel(sheet);
this.setModel(lm);
setFixedCellWidth(50);
setCellRenderer(new RowHeaderRenderer(sheet, table, extraHeight));
}
}

View File

@ -0,0 +1,241 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.view;
import org.apache.poi.hssf.view.brush.PendingPaintings;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
/**
* This class is a table that represents the values in a single worksheet.
*
* @author Ken Arnold, Industrious Media LLC
*/
public class SVSheetTable extends JTable {
private final HSSFSheet sheet;
private final PendingPaintings pendingPaintings;
private FormulaDisplayListener formulaListener;
private JScrollPane scroll;
private static final Color HEADER_BACKGROUND = new Color(235, 235, 235);
/**
* This field is the magic number to convert from a Character width to a java
* pixel width.
* <p/>
* When the "normal" font size in a workbook changes, this effects all of the
* heights and widths. Unfortunately there is no way to retrieve this
* information, hence the MAGIC number.
* <p/>
* This number may only work for the normal style font size of Arial size 10.
*/
private static final int magicCharFactor = 7;
private class HeaderCell extends JLabel {
private final int row;
public HeaderCell(Object value, int row) {
super(value.toString(), CENTER);
this.row = row;
setBackground(HEADER_BACKGROUND);
setOpaque(true);
setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
setRowSelectionAllowed(false);
}
@Override
public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
if (row >= 0) {
d.height = getRowHeight(row);
}
return d;
}
@Override
public Dimension getMaximumSize() {
Dimension d = super.getMaximumSize();
if (row >= 0) {
d.height = getRowHeight(row);
}
return d;
}
@Override
public Dimension getMinimumSize() {
Dimension d = super.getMinimumSize();
if (row >= 0) {
d.height = getRowHeight(row);
}
return d;
}
}
private class HeaderCellRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
return new HeaderCell(value, row);
}
}
private class FormulaDisplayListener implements ListSelectionListener {
private final JTextComponent formulaDisplay;
public FormulaDisplayListener(JTextComponent formulaDisplay) {
this.formulaDisplay = formulaDisplay;
}
public void valueChanged(ListSelectionEvent e) {
int row = getSelectedRow();
int col = getSelectedColumn();
if (row < 0 || col < 0) {
return;
}
if (e.getValueIsAdjusting()) {
return;
}
HSSFCell cell = (HSSFCell) getValueAt(row, col);
String formula = "";
if (cell != null) {
if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
formula = cell.getCellFormula();
} else {
formula = cell.toString();
}
if (formula == null)
formula = "";
}
formulaDisplay.setText(formula);
}
}
public SVSheetTable(HSSFSheet sheet) {
super(new SVTableModel(sheet));
this.sheet = sheet;
setIntercellSpacing(new Dimension(0, 0));
setAutoResizeMode(AUTO_RESIZE_OFF);
JTableHeader header = getTableHeader();
header.setDefaultRenderer(new HeaderCellRenderer());
pendingPaintings = new PendingPaintings(this);
//Set the columns the correct size
TableColumnModel columns = getColumnModel();
for (int i = 0; i < columns.getColumnCount(); i++) {
TableColumn column = columns.getColumn(i);
int width = sheet.getColumnWidth(i);
//256 is because the width is in 256ths of a character
column.setPreferredWidth(width / 256 * magicCharFactor);
}
Toolkit t = getToolkit();
int res = t.getScreenResolution();
TableModel model = getModel();
for (int i = 0; i < model.getRowCount(); i++) {
Row row = sheet.getRow(i - sheet.getFirstRowNum());
if (row != null) {
short h = row.getHeight();
int height = Math.round(Math.max(1, h / (res / 70 * 20) + 3));
System.out.printf("%d: %d (%d @ %d)%n", i, height, h, res);
setRowHeight(i, height);
}
}
addHierarchyListener(new HierarchyListener() {
public void hierarchyChanged(HierarchyEvent e) {
if ((e.getChangeFlags() & HierarchyEvent.PARENT_CHANGED) != 0) {
Container changedParent = e.getChangedParent();
if (changedParent instanceof JViewport) {
Container grandparent = changedParent.getParent();
if (grandparent instanceof JScrollPane) {
JScrollPane jScrollPane = (JScrollPane) grandparent;
setupScroll(jScrollPane);
}
}
}
}
});
}
public void setupScroll(JScrollPane scroll) {
if (scroll == this.scroll)
return;
this.scroll = scroll;
if (scroll == null)
return;
SVRowHeader rowHeader = new SVRowHeader(sheet, this, 0);
scroll.setRowHeaderView(rowHeader);
scroll.setCorner(JScrollPane.UPPER_LEADING_CORNER, headerCell("?"));
}
public void setFormulaDisplay(JTextComponent formulaDisplay) {
ListSelectionModel rowSelMod = getSelectionModel();
ListSelectionModel colSelMod = getColumnModel().getSelectionModel();
if (formulaDisplay == null) {
rowSelMod.removeListSelectionListener(formulaListener);
colSelMod.removeListSelectionListener(formulaListener);
formulaListener = null;
}
if (formulaDisplay != null) {
formulaListener = new FormulaDisplayListener(formulaDisplay);
rowSelMod.addListSelectionListener(formulaListener);
colSelMod.addListSelectionListener(formulaListener);
}
}
public JTextComponent getFormulaDisplay() {
if (formulaListener == null)
return null;
else
return formulaListener.formulaDisplay;
}
public Component headerCell(String text) {
return new HeaderCell(text, -1);
}
@Override
public void paintComponent(Graphics g1) {
Graphics2D g = (Graphics2D) g1;
pendingPaintings.clear();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
super.paintComponent(g);
pendingPaintings.paint(g);
}
}

View File

@ -0,0 +1,203 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.view;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
/**
* Sheet Viewer Table Cell Editor -- not commented via javadoc as it
* nearly completely consists of overridden methods.
*
* @author Jason Height
*/
public class SVTableCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
private static final Color black = getAWTColor(new HSSFColor.BLACK());
private static final Color white = getAWTColor(new HSSFColor.WHITE());
private Map<Integer,HSSFColor> colors = HSSFColor.getIndexHash();
private HSSFWorkbook wb;
private JTextField editor;
private HSSFCell editorValue;
public SVTableCellEditor(HSSFWorkbook wb) {
this.wb = wb;
this.editor = new JTextField();
}
/**
* Gets the cellEditable attribute of the SVTableCellEditor object
*
* @return The cellEditable value
*/
public boolean isCellEditable(java.util.EventObject e) {
if (e instanceof MouseEvent) {
return ((MouseEvent) e).getClickCount() >= 2;
}
return false;
}
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
public boolean startCellEditing(EventObject anEvent) {
System.out.println("Start Cell Editing");
return true;
}
public boolean stopCellEditing() {
System.out.println("Stop Cell Editing");
fireEditingStopped();
return true;
}
public void cancelCellEditing() {
System.out.println("Cancel Cell Editing");
fireEditingCanceled();
}
public void actionPerformed(ActionEvent e) {
System.out.println("Action performed");
stopCellEditing();
}
/**
* Gets the cellEditorValue attribute of the SVTableCellEditor object
*
* @return The cellEditorValue value
*/
public Object getCellEditorValue() {
System.out.println("GetCellEditorValue");
//JMH Look at when this method is called. Should it return a HSSFCell?
return editor.getText();
}
/**
* Gets the tableCellEditorComponent attribute of the SVTableCellEditor object
*
* @return The tableCellEditorComponent value
*/
public Component getTableCellEditorComponent(JTable table, Object value,
boolean isSelected,
int row,
int column) {
System.out.println("GetTableCellEditorComponent");
HSSFCell cell = (HSSFCell) value;
if (cell != null) {
HSSFCellStyle style = cell.getCellStyle();
HSSFFont f = wb.getFontAt(style.getFontIndex());
boolean isbold = f.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL;
boolean isitalics = f.getItalic();
int fontstyle = Font.PLAIN;
if (isbold) fontstyle = Font.BOLD;
if (isitalics) fontstyle = fontstyle | Font.ITALIC;
int fontheight = f.getFontHeightInPoints();
if (fontheight == 9) fontheight = 10; //fix for stupid ol Windows
Font font = new Font(f.getFontName(),fontstyle,fontheight);
editor.setFont(font);
if (style.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) {
editor.setBackground(getAWTColor(style.getFillForegroundColor(), white));
} else editor.setBackground(white);
editor.setForeground(getAWTColor(f.getColor(), black));
//Set the value that is rendered for the cell
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_BLANK:
editor.setText("");
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
if (cell.getBooleanCellValue()) {
editor.setText("true");
} else {
editor.setText("false");
}
break;
case HSSFCell.CELL_TYPE_NUMERIC:
editor.setText(Double.toString(cell.getNumericCellValue()));
break;
case HSSFCell.CELL_TYPE_STRING:
editor.setText(cell.getRichStringCellValue().getString());
break;
case HSSFCell.CELL_TYPE_FORMULA:
default:
editor.setText("?");
}
switch (style.getAlignment()) {
case HSSFCellStyle.ALIGN_LEFT:
case HSSFCellStyle.ALIGN_JUSTIFY:
case HSSFCellStyle.ALIGN_FILL:
editor.setHorizontalAlignment(SwingConstants.LEFT);
break;
case HSSFCellStyle.ALIGN_CENTER:
case HSSFCellStyle.ALIGN_CENTER_SELECTION:
editor.setHorizontalAlignment(SwingConstants.CENTER);
break;
case HSSFCellStyle.ALIGN_GENERAL:
case HSSFCellStyle.ALIGN_RIGHT:
editor.setHorizontalAlignment(SwingConstants.RIGHT);
break;
default:
editor.setHorizontalAlignment(SwingConstants.LEFT);
break;
}
}
return editor;
}
/** This method retrieves the AWT Color representation from the colour hash table
*
*/
private final Color getAWTColor(int index, Color deflt) {
HSSFColor clr = (HSSFColor)colors.get(Integer.valueOf(index));
if (clr == null) return deflt;
return getAWTColor(clr);
}
private static final Color getAWTColor(HSSFColor clr) {
short[] rgb = clr.getTriplet();
return new Color(rgb[0],rgb[1],rgb[2]);
}
}

View File

@ -0,0 +1,274 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.view;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.border.*;
import java.awt.Component;
import java.awt.Color;
import java.awt.Rectangle;
import java.io.Serializable;
import java.text.*;
import org.apache.poi.hssf.usermodel.*;
/**
* Sheet Viewer Table Cell Render -- not commented via javadoc as it
* nearly completely consists of overridden methods.
*
* @author Andrew C. Oliver
*/
public class SVTableCellRenderer extends JLabel
implements TableCellRenderer, Serializable
{
protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
protected SVBorder cellBorder = new SVBorder();
private HSSFWorkbook wb = null;
/** This class holds the references to the predefined cell formats.
*/
private class CellFormatter {
private Format[] textFormatter;
private DecimalFormat generalNumberFormat = new DecimalFormat("0");
public CellFormatter() {
textFormatter = new Format[0x31];
textFormatter[0x01] = new DecimalFormat("0");
textFormatter[0x02] = new DecimalFormat("0.00");
textFormatter[0x03] = new DecimalFormat("#,##0");
textFormatter[0x04] = new DecimalFormat("#,##0.00");
textFormatter[0x05] = new DecimalFormat("$#,##0;$#,##0");
textFormatter[0x06] = new DecimalFormat("$#,##0;$#,##0");
textFormatter[0x07] = new DecimalFormat("$#,##0.00;$#,##0.00");
textFormatter[0x08] = new DecimalFormat("$#,##0.00;$#,##0.00");
textFormatter[0x09] = new DecimalFormat("0%");
textFormatter[0x0A] = new DecimalFormat("0.00%");
textFormatter[0x0B] = new DecimalFormat("0.00E0");
textFormatter[0x0C] = new SVFractionalFormat("# ?/?");
textFormatter[0x0D] = new SVFractionalFormat("# ??/??");
textFormatter[0x0E] = new SimpleDateFormat("M/d/yy");
textFormatter[0x0F] = new SimpleDateFormat("d-MMM-yy");
textFormatter[0x10] = new SimpleDateFormat("d-MMM");
textFormatter[0x11] = new SimpleDateFormat("MMM-yy");
textFormatter[0x12] = new SimpleDateFormat("h:mm a");
textFormatter[0x13] = new SimpleDateFormat("h:mm:ss a");
textFormatter[0x14] = new SimpleDateFormat("h:mm");
textFormatter[0x15] = new SimpleDateFormat("h:mm:ss");
textFormatter[0x16] = new SimpleDateFormat("M/d/yy h:mm");
// 0x17 - 0x24 reserved for international and undocumented 0x25, "(#,##0_);(#,##0)"
//start at 0x26
//jmh need to do colour
//"(#,##0_);[Red](#,##0)"
textFormatter[0x26] = new DecimalFormat("#,##0;#,##0");
//jmh need to do colour
//(#,##0.00_);(#,##0.00)
textFormatter[0x27] = new DecimalFormat("#,##0.00;#,##0.00");
textFormatter[0x28] = new DecimalFormat("#,##0.00;#,##0.00");
//?? textFormatter[0x29] = new DecimalFormat("_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)");
//?? textFormatter[0x2A] = new DecimalFormat("_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)");
//?? textFormatter[0x2B] = new DecimalFormat("_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)");
//?? textFormatter[0x2C] = new DecimalFormat("_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)");
textFormatter[0x2D] = new SimpleDateFormat("mm:ss");
//?? textFormatter[0x2E] = new SimpleDateFormat("[h]:mm:ss");
textFormatter[0x2F] = new SimpleDateFormat("mm:ss.0");
textFormatter[0x30] = new DecimalFormat("##0.0E0");
}
public String format(short index, Object value) {
if (index == 0)
return value.toString();
if (textFormatter[index] == null)
throw new RuntimeException("Sorry. I cant handle the format code :"+Integer.toHexString(index));
return textFormatter[index].format(value);
}
public String format(short index, double value) {
if ( index <= 0 )
return generalNumberFormat.format(value);
if (textFormatter[index] == null)
throw new RuntimeException("Sorry. I cant handle the format code :"+Integer.toHexString(index));
if (textFormatter[index] instanceof DecimalFormat) {
return ((DecimalFormat)textFormatter[index]).format(value);
}
if (textFormatter[index] instanceof SVFractionalFormat) {
return ((SVFractionalFormat)textFormatter[index]).format(value);
}
throw new RuntimeException("Sorry. I cant handle a non decimal formatter for a decimal value :"+Integer.toHexString(index));
}
public boolean useRedColor(short index, double value) {
return (((index == 0x06)||(index == 0x08)||(index == 0x26) || (index == 0x27)) && (value < 0));
}
}
private final CellFormatter cellFormatter = new CellFormatter();
public SVTableCellRenderer(HSSFWorkbook wb) {
super();
setOpaque(true);
setBorder(noFocusBorder);
this.wb = wb;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
boolean isBorderSet = false;
//If the JTables default cell renderer has been setup correctly the
//value will be the HSSFCell that we are trying to render
HSSFCell c = (HSSFCell)value;
if (c != null) {
HSSFCellStyle s = c.getCellStyle();
HSSFFont f = wb.getFontAt(s.getFontIndex());
setFont(SVTableUtils.makeFont(f));
if (s.getFillPattern() == HSSFCellStyle.SOLID_FOREGROUND) {
setBackground(SVTableUtils.getAWTColor(s.getFillForegroundColor(), SVTableUtils.white));
} else setBackground(SVTableUtils.white);
setForeground(SVTableUtils.getAWTColor(f.getColor(), SVTableUtils.black));
cellBorder.setBorder(SVTableUtils.getAWTColor(s.getTopBorderColor(), SVTableUtils.black),
SVTableUtils.getAWTColor(s.getRightBorderColor(), SVTableUtils.black),
SVTableUtils.getAWTColor(s.getBottomBorderColor(), SVTableUtils.black),
SVTableUtils.getAWTColor(s.getLeftBorderColor(), SVTableUtils.black),
s.getBorderTop(), s.getBorderRight(),
s.getBorderBottom(), s.getBorderLeft(),
hasFocus);
setBorder(cellBorder);
isBorderSet=true;
//Set the value that is rendered for the cell
switch (c.getCellType()) {
case HSSFCell.CELL_TYPE_BLANK:
setValue("");
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
if (c.getBooleanCellValue()) {
setValue("true");
} else {
setValue("false");
}
break;
case HSSFCell.CELL_TYPE_NUMERIC:
short format = s.getDataFormat();
double numericValue = c.getNumericCellValue();
if (cellFormatter.useRedColor(format, numericValue))
setForeground(Color.red);
else setForeground(null);
setValue(cellFormatter.format(format, c.getNumericCellValue()));
break;
case HSSFCell.CELL_TYPE_STRING:
setValue(c.getRichStringCellValue().getString());
break;
case HSSFCell.CELL_TYPE_FORMULA:
default:
setValue("?");
}
//Set the text alignment of the cell
switch (s.getAlignment()) {
case HSSFCellStyle.ALIGN_LEFT:
case HSSFCellStyle.ALIGN_JUSTIFY:
case HSSFCellStyle.ALIGN_FILL:
setHorizontalAlignment(SwingConstants.LEFT);
break;
case HSSFCellStyle.ALIGN_CENTER:
case HSSFCellStyle.ALIGN_CENTER_SELECTION:
setHorizontalAlignment(SwingConstants.CENTER);
break;
case HSSFCellStyle.ALIGN_GENERAL:
case HSSFCellStyle.ALIGN_RIGHT:
setHorizontalAlignment(SwingConstants.RIGHT);
break;
default:
setHorizontalAlignment(SwingConstants.LEFT);
break;
}
} else {
setValue("");
setBackground(SVTableUtils.white);
}
if (hasFocus) {
if (!isBorderSet) {
//This is the border to paint when there is no border
//and the cell has focus
cellBorder.setBorder(SVTableUtils.black,
SVTableUtils.black,
SVTableUtils.black,
SVTableUtils.black,
HSSFCellStyle.BORDER_NONE,
HSSFCellStyle.BORDER_NONE,
HSSFCellStyle.BORDER_NONE,
HSSFCellStyle.BORDER_NONE,
isSelected);
setBorder(cellBorder);
}
if (table.isCellEditable(row, column)) {
setForeground( UIManager.getColor("Table.focusCellForeground") );
setBackground( UIManager.getColor("Table.focusCellBackground") );
}
} else if (!isBorderSet) {
setBorder(noFocusBorder);
}
// ---- begin optimization to avoid painting background ----
Color back = getBackground();
boolean colorMatch = (back != null) && ( back.equals(table.getBackground()) ) && table.isOpaque();
setOpaque(!colorMatch);
// ---- end optimization to aviod painting background ----
return this;
}
public void validate() {}
public void revalidate() {}
public void repaint(long tm, int x, int y, int width, int height) {}
public void repaint(Rectangle r) { }
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
// Strings get interned...
if (propertyName=="text") {
super.firePropertyChange(propertyName, oldValue, newValue);
}
}
public void firePropertyChange(String propertyName, boolean oldValue, boolean newValue) { }
/**
* Sets the string to either the value or "" if the value is null.
*
*/
protected void setValue(Object value) {
setText((value == null) ? "" : value.toString());
}
}

View File

@ -0,0 +1,87 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.view;
import java.util.Iterator;
import javax.swing.table.*;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFCell;
/**
* Sheet Viewer Table Model - The model for the Sheet Viewer just overrides things.
* @author Andrew C. Oliver
*/
public class SVTableModel extends AbstractTableModel {
private HSSFSheet st = null;
int maxcol = 0;
public SVTableModel(HSSFSheet st, int maxcol) {
this.st = st;
this.maxcol=maxcol;
}
public SVTableModel(HSSFSheet st) {
this.st = st;
Iterator i = st.rowIterator();
while (i.hasNext()) {
HSSFRow row = (HSSFRow)i.next();
if (maxcol < (row.getLastCellNum()+1)) {
this.maxcol = row.getLastCellNum();
}
}
}
public int getColumnCount() {
return this.maxcol+1;
}
public Object getValueAt(int row, int col) {
HSSFRow r = st.getRow(row);
HSSFCell c = null;
if (r != null) {
c = r.getCell(col);
}
return c;
}
public int getRowCount() {
return st.getLastRowNum() + 1;
}
public Class getColumnClass(int c) {
return HSSFCell.class;
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
if (aValue != null)
System.out.println("SVTableModel.setValueAt. value type = "+aValue.getClass().getName());
else System.out.println("SVTableModel.setValueAt. value type = null");
}
}

View File

@ -0,0 +1,93 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.view;
import java.util.*;
import java.awt.*;
import javax.swing.border.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*;
/**
* SVTableCell Editor and Renderer helper functions.
*
* @author Jason Height
*/
public class SVTableUtils {
private final static Map<Integer,HSSFColor> colors = HSSFColor.getIndexHash();
/** Description of the Field */
public final static Color black = getAWTColor(new HSSFColor.BLACK());
/** Description of the Field */
public final static Color white = getAWTColor(new HSSFColor.WHITE());
/** Description of the Field */
public static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
/**
* Creates a new font for a specific cell style
*/
public static Font makeFont(HSSFFont font) {
boolean isbold = font.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL;
boolean isitalics = font.getItalic();
int fontstyle = Font.PLAIN;
if (isbold) {
fontstyle = Font.BOLD;
}
if (isitalics) {
fontstyle = fontstyle | Font.ITALIC;
}
int fontheight = font.getFontHeightInPoints();
if (fontheight == 9) {
//fix for stupid ol Windows
fontheight = 10;
}
return new Font(font.getFontName(), fontstyle, fontheight);
}
/**
* This method retrieves the AWT Color representation from the colour hash table
*
* @param index Description of the Parameter
* @param deflt Description of the Parameter
* @return The aWTColor value
*/
public final static Color getAWTColor(int index, Color deflt) {
HSSFColor clr = (HSSFColor) colors.get(Integer.valueOf(index));
if (clr == null) {
return deflt;
}
return getAWTColor(clr);
}
/**
* Gets the aWTColor attribute of the SVTableUtils class
*
* @param clr Description of the Parameter
* @return The aWTColor value
*/
public final static Color getAWTColor(HSSFColor clr) {
short[] rgb = clr.getTriplet();
return new Color(rgb[0], rgb[1], rgb[2]);
}
}

View File

@ -0,0 +1,172 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.view;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* Sheet Viewer - Views XLS files via HSSF. Can be used as an applet with
* filename="" or as a applications (pass the filename as the first parameter).
* Or you can pass it a URL in a "url" parameter when run as an applet or just
* that first parameter must start with http:// and it will guess its a url. I
* only tested it as an applet though, so it probably won't work...you fix it.
*
* @author Andrew C. Oliver
* @author Jason Height
*/
public class SViewer extends JApplet {
private SViewerPanel panel;
boolean isStandalone = false;
String filename = null;
/**Get a parameter value*/
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**Construct the applet*/
public SViewer() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
/**Component initialization*/
private void jbInit() throws Exception {
InputStream i = null;
boolean isurl = false;
if (filename == null) filename = getParameter("filename");
if (filename == null || filename.substring(0,7).equals("http://")) {
isurl = true;
if (filename == null) filename = getParameter("url");
i = getXLSFromURL(filename);
}
HSSFWorkbook wb = null;
if (isurl) {
wb = constructWorkbook(i);
} else {
wb = constructWorkbook(filename);
}
panel = new SViewerPanel(wb, false);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel, BorderLayout.CENTER);
}
private HSSFWorkbook constructWorkbook(String filename) throws FileNotFoundException, IOException {
HSSFWorkbook wb = null;
FileInputStream in = new FileInputStream(filename);
wb = new HSSFWorkbook(in);
in.close();
return wb;
}
private HSSFWorkbook constructWorkbook(InputStream in) throws IOException {
HSSFWorkbook wb = null;
wb = new HSSFWorkbook(in);
in.close();
return wb;
}
/**Start the applet*/
public void start() {
}
/**Stop the applet*/
public void stop() {
}
/**Destroy the applet*/
public void destroy() {
}
/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo() {
return null;
}
/**
* opens a url and returns an inputstream
*
*/
private InputStream getXLSFromURL(String urlstring) throws MalformedURLException, IOException {
URL url = new URL(urlstring);
URLConnection uc = url.openConnection();
String field = uc.getHeaderField(0);
for (int i=0;field != null; i++) {
System.out.println(field);
field = uc.getHeaderField(i);
}
BufferedInputStream is = new BufferedInputStream(uc.getInputStream());
return is;
}
/**Main method*/
public static void main(String[] args) {
if(args.length < 1) {
throw new IllegalArgumentException("A filename to view must be supplied as the first argument, but none was given");
}
SViewer applet = new SViewer();
applet.isStandalone = true;
applet.filename = args[0];
Frame frame;
frame = new Frame() {
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public synchronized void setTitle(String title) {
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.setTitle("Applet Frame");
frame.add(applet, BorderLayout.CENTER);
applet.init();
applet.start();
frame.setSize(400,320);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
}
}

View File

@ -0,0 +1,292 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.view;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.table.*;
import org.apache.poi.hssf.usermodel.*;
/**
* This class presents the sheets to the user.
*
*
* @author Andrew C. Oliver
* @author Jason Height
*/
public class SViewerPanel extends JPanel {
/** This field is the magic number to convert from a Character width to a
* java pixel width.
*
* When the "normal" font size in a workbook changes, this effects all
* of the heights and widths. Unfortunately there is no way to retrieve this
* information, hence the MAGIC number.
*
* This number may only work for the normal style font size of Arial size 10.
*
*/
private static final int magicCharFactor = 7;
/** Reference to the wookbook that is being displayed*/
/* package */ HSSFWorkbook wb;
/** Reference to the tabs component*/
/* package */ JTabbedPane sheetPane;
/** Reference to the cell renderer that is used to render all cells*/
private SVTableCellRenderer cellRenderer;
/** Reference to the cell editor that is used to edit all cells.
* Only constructed if editing is allowed
*/
private SVTableCellEditor cellEditor;
/** Flag indicating if editing is allowed. Otherwise the viewer is in
* view only mode.
*/
private boolean allowEdits;
/**Construct the representation of the workbook*/
public SViewerPanel(HSSFWorkbook wb, boolean allowEdits) {
this.wb = wb;
this.allowEdits = allowEdits;
initialiseGui();
}
private void initialiseGui() {
cellRenderer = new SVTableCellRenderer(this.wb);
if (allowEdits)
cellEditor = new SVTableCellEditor(this.wb);
//Initialise the Panel
sheetPane = new JTabbedPane(JTabbedPane.BOTTOM);
if (allowEdits)
sheetPane.addMouseListener(createTabListener());
int sheetCount = wb.getNumberOfSheets();
for (int i=0; i<sheetCount;i++) {
String sheetName = wb.getSheetName(i);
//Add the new sheet to the tabbed pane
sheetPane.addTab(sheetName, makeSheetView(wb.getSheetAt(i)));
}
setLayout(new BorderLayout());
add(sheetPane, BorderLayout.CENTER);
}
protected JComponent makeSheetView(HSSFSheet sheet) {
JTable sheetView = new JTable(new SVTableModel(sheet));
sheetView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
sheetView.setDefaultRenderer(HSSFCell.class, cellRenderer);
if (allowEdits)
sheetView.setDefaultEditor(HSSFCell.class, cellEditor);
JTableHeader header = sheetView.getTableHeader();
//Dont allow column reordering
header.setReorderingAllowed(false);
//Only allow column resizing if editing is allowed
header.setResizingAllowed(allowEdits);
//Set the columns the correct size
TableColumnModel columns = sheetView.getColumnModel();
for (int i=0; i< columns.getColumnCount(); i++) {
TableColumn column = columns.getColumn(i);
int width = sheet.getColumnWidth(i);
//256 is because the width is in 256ths of a character
column.setPreferredWidth(width/256*magicCharFactor);
}
//Set the rows to the correct size
int rows = sheet.getPhysicalNumberOfRows();
Insets insets = cellRenderer.getInsets();
//Need to include the insets in the calculation of the row height to use.
int extraHeight = insets.bottom+insets.top;
for (int i=0; i< rows; i++) {
HSSFRow row = sheet.getRow(i);
if (row == null) {
sheetView.setRowHeight(i, (int)sheet.getDefaultRowHeightInPoints()+extraHeight);
} else {
sheetView.setRowHeight(i, (int)row.getHeightInPoints()+extraHeight);
}
}
//Add the row header to the sheet
SVRowHeader rowHeader = new SVRowHeader(sheet, sheetView, extraHeight);
JScrollPane scroll = new JScrollPane( sheetView );
scroll.setRowHeaderView(rowHeader);
return scroll;
}
public void paint(Graphics g) {
//JMH I am only overriding this to get a picture of the time taken to paint
long start = System.currentTimeMillis();
super.paint(g);
long elapsed = System.currentTimeMillis()-start;
System.out.println("Paint time = "+elapsed);
}
protected MouseListener createTabListener() {
return new TabListener();
}
/** This class defines the default MouseListener that listens to
* mouse events in the tabbed pane
*
* The default is to popup a menu when the event occurs over a tab
*/
private class TabListener implements MouseListener {
public JPopupMenu popup;
public TabListener() {
popup = new JPopupMenu("Sheet");
popup.add(createInsertSheetAction());
popup.add(createDeleteSheetAction());
popup.add(createRenameSheetAction());
}
protected Action createInsertSheetAction() {
return new InsertSheetAction();
}
protected Action createDeleteSheetAction() {
return new DeleteSheetAction();
}
protected Action createRenameSheetAction() {
return new RenameSheetAction();
}
/** This method will display the popup if the mouseevent is a popup event
* and the event occurred over a tab
*/
protected void checkPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
int tab = sheetPane.getUI().tabForCoordinate(sheetPane, e.getX(), e.getY());
if (tab != -1) {
popup.show(sheetPane, e.getX(), e.getY());
}
}
}
public void mouseClicked(MouseEvent e) {
checkPopup(e);
}
public void mousePressed(MouseEvent e) {
checkPopup(e);
}
public void mouseReleased(MouseEvent e) {
checkPopup(e);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
/** This class defines the action that is performed when the sheet is renamed*/
private class RenameSheetAction extends AbstractAction {
public RenameSheetAction() {
super("Rename");
}
public void actionPerformed(ActionEvent e) {
int tabIndex = sheetPane.getSelectedIndex();
if (tabIndex != -1) {
String newSheetName = JOptionPane.showInputDialog(sheetPane, "Enter a new Sheetname", "Rename Sheet", JOptionPane.QUESTION_MESSAGE);
if (newSheetName != null) {
wb.setSheetName(tabIndex, newSheetName);
sheetPane.setTitleAt(tabIndex, newSheetName);
}
}
}
}
/** This class defines the action that is performed when a sheet is inserted*/
private class InsertSheetAction extends AbstractAction {
public InsertSheetAction() {
super("Insert");
}
public void actionPerformed(ActionEvent e) {
//Create a new sheet then search for the sheet and make sure that the
//sheetPane shows it.
HSSFSheet newSheet = wb.createSheet();
for (int i=0; i<wb.getNumberOfSheets();i++) {
HSSFSheet sheet = wb.getSheetAt(i);
if (newSheet == sheet) {
sheetPane.insertTab(wb.getSheetName(i), null, makeSheetView(sheet), null, i);
}
}
}
}
/** This class defines the action that is performed when the sheet is deleted*/
private class DeleteSheetAction extends AbstractAction {
public DeleteSheetAction() {
super("Delete");
}
public void actionPerformed(ActionEvent e) {
int tabIndex = sheetPane.getSelectedIndex();
if (tabIndex != -1) {
if (JOptionPane.showConfirmDialog(sheetPane, "Are you sure that you want to delete the selected sheet", "Delete Sheet?", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
wb.removeSheetAt(tabIndex);
sheetPane.remove(tabIndex);
}
}
}
}
public boolean isEditable() {
return allowEdits;
}
/**Main method*/
public static void main(String[] args) {
if(args.length < 1) {
throw new IllegalArgumentException("A filename to view must be supplied as the first argument, but none was given");
}
try {
FileInputStream in = new FileInputStream(args[0]);
HSSFWorkbook wb = new HSSFWorkbook(in);
in.close();
SViewerPanel p = new SViewerPanel(wb, true);
JFrame frame;
frame = new JFrame() {
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public synchronized void setTitle(String title) {
super.setTitle(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
};
frame.setTitle("Viewer Frame");
frame.getContentPane().add(p, BorderLayout.CENTER);
frame.setSize(800,640);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2);
frame.setVisible(true);
} catch (IOException ex) {
ex.printStackTrace();
System.exit(1);
}
}
}

View File

@ -0,0 +1,72 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.view.brush;
import java.awt.*;
/**
* This is a basic brush that just draws the line with the given parameters.
* This is a {@link BasicStroke} object that can be used as a {@link Brush}.
*
* @author Ken Arnold, Industrious Media LLC
* @see BasicStroke
*/
public class BasicBrush extends BasicStroke implements Brush {
/**
* Creates a new basic brush with the given width. Invokes {@link
* BasicStroke#BasicStroke(float)}
*
* @param width The brush width.
*
* @see BasicStroke#BasicStroke(float)
*/
public BasicBrush(float width) {
super(width);
}
/**
* Creates a new basic brush with the given width, cap, and join. Invokes
* {@link BasicStroke#BasicStroke(float,int,int)}
*
* @param width The brush width.
* @param cap The capping style.
* @param join The join style.
*
* @see BasicStroke#BasicStroke(float, int, int)
*/
public BasicBrush(float width, int cap, int join) {
super(width, cap, join);
}
/**
* Creates a new basic brush with the given parameters. Invokes {@link
* BasicStroke#BasicStroke(float,int,int,float,float[],float)} with a miter
* limit of 11 (the normal default value).
*
* @param width The brush width.
* @param cap The capping style.
* @param join The join style.
* @param dashes The dash intervals.
* @param dashPos The intial dash position in the dash intervals.
*
* @see BasicStroke#BasicStroke(float, int, int, float, float[], float)
*/
public BasicBrush(float width, int cap, int join, float[] dashes,
int dashPos) {
super(width, cap, join, 11.0f, dashes, dashPos);
}
}

View File

@ -0,0 +1,31 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.view.brush;
import java.awt.*;
/**
* This is the type you must implement to create a brush that will be used for a
* spreadsheet border.
*
* @author Ken Arnold, Industrious Media LLC
*/
public interface Brush extends Stroke {
/** Returns the width of the brush. */
float getLineWidth();
}

View File

@ -0,0 +1,62 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.view.brush;
import java.awt.*;
/**
* This Stroke implementation applies a BasicStroke to a shape twice. If you
* draw with this Stroke, then instead of outlining the shape, you're outlining
* the outline of the shape.
*
* @author Ken Arnold, Industrious Media LLC
*/
public class DoubleStroke implements Brush {
BasicStroke stroke1, stroke2; // the two strokes to use
/**
* Creates a new double-stroke brush. This surrounds a cell with a two
* lines separated by white space between.
*
* @param width1 The width of the blank space in the middle
* @param width2 The width of the each of the two drawn strokes.
*/
public DoubleStroke(float width1, float width2) {
stroke1 = new BasicStroke(width1); // Constructor arguments specify
stroke2 = new BasicStroke(width2); // the line widths for the strokes
}
/**
* Stroke the outline.
*
* @param s The shape in which to stroke.
*
* @return The created stroke as a new shape.
*/
public Shape createStrokedShape(Shape s) {
// Use the first stroke to create an outline of the shape
Shape outline = stroke1.createStrokedShape(s);
// Use the second stroke to create an outline of that outline.
// It is this outline of the outline that will be filled in
return stroke2.createStrokedShape(outline);
}
/** {@inheritDoc} */
public float getLineWidth() {
return stroke1.getLineWidth() + 2 * stroke2.getLineWidth();
}
}

View File

@ -0,0 +1,178 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hssf.view.brush;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.ArrayList;
import java.util.List;
/**
* This class is used to hold pending brush paintings. The model is that some
* border drawing requires drawing strokes after all the cells have been
* painted. The list of pending paintings can be put in this object during the
* initial paint of the component, and then executed at the appropriate time,
* such as at the end of the containing object's {@link
* JComponent#paintChildren(Graphics)} method.
* <p/>
* It is up to the parent component to invoke the {@link #paint(Graphics2D)}
* method of this objet at that appropriate time.
*
* @author Ken Arnold, Industrious Media LLC
*/
public class PendingPaintings {
/**
* The name of the client property that holds this object in the parent
* component.
*/
public static final String PENDING_PAINTINGS =
PendingPaintings.class.getSimpleName();
private final List<Painting> paintings;
/** A single painting description. */
public static class Painting {
final Stroke stroke;
final Color color;
final Shape shape;
final AffineTransform transform;
/**
* Creates a new painting description.
*
* @param stroke The stroke to paint.
* @param color The color of the stroke.
* @param shape The shape of the stroke.
* @param transform The transformation matrix to use.
*/
public Painting(Stroke stroke, Color color, Shape shape,
AffineTransform transform) {
this.color = color;
this.shape = shape;
this.stroke = stroke;
this.transform = transform;
}
/**
* Draw the painting.
*
* @param g The graphics object to use to draw with.
*/
public void draw(Graphics2D g) {
g.setTransform(transform);
g.setStroke(stroke);
g.setColor(color);
g.draw(shape);
}
}
/**
* Creates a new object on the given parent. The created object will be
* stored as a client property.
*
* @param parent
*/
public PendingPaintings(JComponent parent) {
paintings = new ArrayList<Painting>();
parent.putClientProperty(PENDING_PAINTINGS, this);
}
/** Drops all pending paintings. */
public void clear() {
paintings.clear();
}
/**
* Paints all pending paintings. Once they have been painted they are
* removed from the list of pending paintings (they aren't pending anymore,
* after all).
*
* @param g The graphics object to draw with.
*/
public void paint(Graphics2D g) {
g.setBackground(Color.CYAN);
AffineTransform origTransform = g.getTransform();
for (Painting c : paintings) {
c.draw(g);
}
g.setTransform(origTransform);
clear();
}
/**
* Adds a new pending painting to the list on the given component. This
* will find the first ancestor that has a {@link PendingPaintings} client
* property, starting with the component itself.
*
* @param c The component for which the painting is being added.
* @param g The graphics object to draw with.
* @param stroke The stroke to draw.
* @param color The color to draw with.
* @param shape The shape to stroke.
*/
public static void add(JComponent c, Graphics2D g, Stroke stroke,
Color color, Shape shape) {
add(c, new Painting(stroke, color, shape, g.getTransform()));
}
/**
* Adds a new pending painting to the list on the given component. This
* will find the first ancestor that has a {@link PendingPaintings} client
* property, starting with the component itself.
*
* @param c The component for which the painting is being added.
* @param newPainting The new painting.
*/
public static void add(JComponent c, Painting newPainting) {
PendingPaintings pending = pendingPaintingsFor(c);
if (pending != null) {
pending.paintings.add(newPainting);
}
}
/**
* Returns the pending painting object for the given component, if any. This
* is retrieved from the first object found that has a {@link
* #PENDING_PAINTINGS} client property, starting with this component and
* looking up its ancestors (parent, parent's parent, etc.)
* <p/>
* This allows any descendant of a component that has a {@link
* PendingPaintings} property to add its own pending paintings.
*
* @param c The component for which the painting is being added.
*
* @return The pending painting object for that component, or <tt>null</tt>
* if there is none.
*/
public static PendingPaintings pendingPaintingsFor(JComponent c) {
for (Component parent = c;
parent != null;
parent = parent.getParent()) {
if (parent instanceof JComponent) {
JComponent jc = (JComponent) parent;
Object pd = jc.getClientProperty(PENDING_PAINTINGS);
if (pd != null)
return (PendingPaintings) pd;
}
}
return null;
}
}

View File

@ -0,0 +1,26 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<!--
====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
====================================================================
-->
<html>
<body>
This package contains some brushes that are used when drawing borders for Excel
cells.
</body>
</html>

View File

@ -0,0 +1,225 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hwpf;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.hwpf.model.*;
import java.io.*;
public final class Word2Forrest
{
Writer _out;
HWPFDocument _doc;
public Word2Forrest(HWPFDocument doc, OutputStream stream)
throws IOException, UnsupportedEncodingException
{
OutputStreamWriter out = new OutputStreamWriter (stream, "UTF-8");
_out = out;
_doc = doc;
init ();
openDocument ();
openBody ();
Range r = doc.getRange ();
StyleSheet styleSheet = doc.getStyleSheet ();
int sectionLevel = 0;
int lenParagraph = r.numParagraphs ();
boolean inCode = false;
for (int x = 0; x < lenParagraph; x++)
{
Paragraph p = r.getParagraph (x);
String text = p.text ();
if (text.trim ().length () == 0)
{
continue;
}
StyleDescription paragraphStyle = styleSheet.getStyleDescription (p.
getStyleIndex ());
String styleName = paragraphStyle.getName();
if (styleName.startsWith ("Heading"))
{
if (inCode)
{
closeSource();
inCode = false;
}
int headerLevel = Integer.parseInt (styleName.substring (8));
if (headerLevel > sectionLevel)
{
openSection ();
}
else
{
for (int y = 0; y < (sectionLevel - headerLevel) + 1; y++)
{
closeSection ();
}
openSection ();
}
sectionLevel = headerLevel;
openTitle ();
writePlainText (text);
closeTitle ();
}
else
{
int cruns = p.numCharacterRuns ();
CharacterRun run = p.getCharacterRun (0);
String fontName = run.getFontName();
if (fontName.startsWith ("Courier"))
{
if (!inCode)
{
openSource ();
inCode = true;
}
writePlainText (p.text());
}
else
{
if (inCode)
{
inCode = false;
closeSource();
}
openParagraph();
writePlainText(p.text());
closeParagraph();
}
}
}
for (int x = 0; x < sectionLevel; x++)
{
closeSection();
}
closeBody();
closeDocument();
_out.flush();
}
public void init ()
throws IOException
{
_out.write ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
_out.write ("<!DOCTYPE document PUBLIC \"-//APACHE//DTD Documentation V1.1//EN\" \"./dtd/document-v11.dtd\">\r\n");
}
public void openDocument ()
throws IOException
{
_out.write ("<document>\r\n");
}
public void closeDocument ()
throws IOException
{
_out.write ("</document>\r\n");
}
public void openBody ()
throws IOException
{
_out.write ("<body>\r\n");
}
public void closeBody ()
throws IOException
{
_out.write ("</body>\r\n");
}
public void openSection ()
throws IOException
{
_out.write ("<section>");
}
public void closeSection ()
throws IOException
{
_out.write ("</section>");
}
public void openTitle ()
throws IOException
{
_out.write ("<title>");
}
public void closeTitle ()
throws IOException
{
_out.write ("</title>");
}
public void writePlainText (String text)
throws IOException
{
_out.write (text);
}
public void openParagraph ()
throws IOException
{
_out.write ("<p>");
}
public void closeParagraph ()
throws IOException
{
_out.write ("</p>");
}
public void openSource ()
throws IOException
{
_out.write ("<source><![CDATA[");
}
public void closeSource ()
throws IOException
{
_out.write ("]]></source>");
}
public static void main(String[] args)
{
try
{
OutputStream out = new FileOutputStream("c:\\test.xml");
new Word2Forrest(new HWPFDocument(new FileInputStream(args[0])), out);
out.close();
}
catch (Throwable t)
{
t.printStackTrace();
}
}
}

View File

@ -0,0 +1,241 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.poifs.poibrowser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.poi.hpsf.ClassID;
/**
* <p>Provides utility methods for encoding and decoding hexadecimal
* data.</p>
*
* @author Rainer Klute (klute@rainer-klute.de) - with portions from Tomcat
*/
public class Codec
{
/**
* <p>The nibbles' hexadecimal values. A nibble is a half byte.</p>
*/
protected static final byte hexval[] =
{(byte) '0', (byte) '1', (byte) '2', (byte) '3',
(byte) '4', (byte) '5', (byte) '6', (byte) '7',
(byte) '8', (byte) '9', (byte) 'A', (byte) 'B',
(byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F'};
/**
* <p>Converts a string into its hexadecimal notation.</p>
*/
public static String hexEncode(final String s)
{
return hexEncode(s.getBytes());
}
/**
* <p>Converts a byte array into its hexadecimal notation.</p>
*/
public static String hexEncode(final byte[] s)
{
return hexEncode(s, 0, s.length);
}
/**
* <p>Converts a part of a byte array into its hexadecimal
* notation.</p>
*/
public static String hexEncode(final byte[] s, final int offset,
final int length)
{
StringBuffer b = new StringBuffer(length * 2);
for (int i = offset; i < offset + length; i++)
{
int c = s[i];
b.append((char) hexval[(c & 0xF0) >> 4]);
b.append((char) hexval[(c & 0x0F) >> 0]);
}
return b.toString();
}
/**
* <p>Converts a single byte into its hexadecimal notation.</p>
*/
public static String hexEncode(final byte b)
{
StringBuffer sb = new StringBuffer(2);
sb.append((char) hexval[(b & 0xF0) >> 4]);
sb.append((char) hexval[(b & 0x0F) >> 0]);
return sb.toString();
}
/**
* <p>Converts a short value (16-bit) into its hexadecimal
* notation.</p>
*/
public static String hexEncode(final short s)
{
StringBuffer sb = new StringBuffer(4);
sb.append((char) hexval[(s & 0xF000) >> 12]);
sb.append((char) hexval[(s & 0x0F00) >> 8]);
sb.append((char) hexval[(s & 0x00F0) >> 4]);
sb.append((char) hexval[(s & 0x000F) >> 0]);
return sb.toString();
}
/**
* <p>Converts an int value (32-bit) into its hexadecimal
* notation.</p>
*/
public static String hexEncode(final int i)
{
StringBuffer sb = new StringBuffer(8);
sb.append((char) hexval[(i & 0xF0000000) >> 28]);
sb.append((char) hexval[(i & 0x0F000000) >> 24]);
sb.append((char) hexval[(i & 0x00F00000) >> 20]);
sb.append((char) hexval[(i & 0x000F0000) >> 16]);
sb.append((char) hexval[(i & 0x0000F000) >> 12]);
sb.append((char) hexval[(i & 0x00000F00) >> 8]);
sb.append((char) hexval[(i & 0x000000F0) >> 4]);
sb.append((char) hexval[(i & 0x0000000F) >> 0]);
return sb.toString();
}
/**
* <p>Converts a long value (64-bit) into its hexadecimal
* notation.</p>
*/
public static String hexEncode(final long l)
{
StringBuffer sb = new StringBuffer(16);
sb.append(hexEncode((int) (l & 0xFFFFFFFF00000000L) >> 32));
sb.append(hexEncode((int) (l & 0x00000000FFFFFFFFL) >> 0));
return sb.toString();
}
/**
* <p>Converts a class ID into its hexadecimal notation.</p>
*/
public static String hexEncode(final ClassID classID)
{
return hexEncode(classID.getBytes());
}
/**
* <p>Decodes the hexadecimal representation of a sequence of
* bytes into a byte array. Each character in the string
* represents a nibble (half byte) and must be one of the
* characters '0'-'9', 'A'-'F' or 'a'-'f'.</p>
*
* @param s The string to be decoded
*
* @return The bytes
*
* @throws IllegalArgumentException if the string does not contain
* a valid representation of a byte sequence.
*/
public static byte[] hexDecode(final String s)
{
final int length = s.length();
/* The string to be converted must have an even number of
characters. */
if (length % 2 == 1)
throw new IllegalArgumentException
("String has odd length " + length);
byte[] b = new byte[length / 2];
char[] c = new char[length];
s.toUpperCase().getChars(0, length, c, 0);
for (int i = 0; i < length; i += 2)
b[i/2] = (byte) (decodeNibble(c[i]) << 4 & 0xF0 |
decodeNibble(c[i+1]) & 0x0F);
return b;
}
/**
* <p>Decodes a nibble.</p>
*
* @param c A character in the range '0'-'9' or 'A'-'F'. Lower
* case is not supported here.
*
* @return The decoded nibble in the range 0-15
*
* @throws IllegalArgumentException if <em>c</em> is not a
* permitted character
*/
protected static byte decodeNibble(final char c)
{
for (byte i = 0; i < hexval.length; i++)
if ((byte) c == hexval[i])
return i;
throw new IllegalArgumentException("\"" + c + "\"" +
" does not represent a nibble.");
}
/**
* <p>For testing.</p>
*/
public static void main(final String args[])
throws IOException
{
final BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
String s;
do
{
s = in.readLine();
if (s != null)
{
String bytes = hexEncode(s);
System.out.print("Hex encoded (String): ");
System.out.println(bytes);
System.out.print("Hex encoded (byte[]): ");
System.out.println(hexEncode(s.getBytes()));
System.out.print("Re-decoded (byte[]): ");
System.out.println(new String(hexDecode(bytes)));
}
}
while (s != null);
}
}

View File

@ -0,0 +1,79 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.poifs.poibrowser;
import java.io.*;
import org.apache.poi.poifs.filesystem.*;
/**
* <p>Describes the most important (whatever that is) features of a
* {@link POIFSDocument}.</p>
*
* @author Rainer Klute <a
* href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
*/
public class DocumentDescriptor
{
String name;
POIFSDocumentPath path;
DocumentInputStream stream;
int size;
byte[] bytes;
/**
* <p>Creates a {@link DocumentDescriptor}.</p>
*
* @param name The stream's name.
*
* @param path The stream's path in the POI filesystem hierarchy.
*
* @param stream The stream.
*
* @param nrOfBytes The maximum number of bytes to display in a
* dump starting at the beginning of the stream.
*/
public DocumentDescriptor(final String name,
final POIFSDocumentPath path,
final DocumentInputStream stream,
final int nrOfBytes)
{
this.name = name;
this.path = path;
this.stream = stream;
try
{
size = stream.available();
if (stream.markSupported())
{
stream.mark(nrOfBytes);
final byte[] b = new byte[nrOfBytes];
final int read = stream.read(b, 0, Math.min(size, b.length));
bytes = new byte[read];
System.arraycopy(b, 0, bytes, 0, read);
stream.reset();
}
}
catch (IOException ex)
{
System.out.println(ex);
}
}
}

View File

@ -0,0 +1,78 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.poifs.poibrowser;
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
/**
* <p>{@link TreeCellRenderer} for a {@link DocumentDescriptor}. The
* renderer is extremly rudimentary since displays only the document's
* name, its size and its fist few bytes.</p>
*
* @author Rainer Klute <a
* href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
*/
public class DocumentDescriptorRenderer extends DefaultTreeCellRenderer
{
public Component getTreeCellRendererComponent(final JTree tree,
final Object value,
final boolean selected,
final boolean expanded,
final boolean leaf,
final int row,
final boolean hasFocus)
{
final DocumentDescriptor d = (DocumentDescriptor)
((DefaultMutableTreeNode) value).getUserObject();
final JPanel p = new JPanel();
final JTextArea text = new JTextArea();
text.append(renderAsString(d));
text.setFont(new Font("Monospaced", Font.PLAIN, 10));
p.add(text);
if (selected)
Util.invert(text);
return p;
}
/**
* <p>Renders {@link DocumentDescriptor} as a string.</p>
*/
protected String renderAsString(final DocumentDescriptor d)
{
final StringBuffer b = new StringBuffer();
b.append("Name: ");
b.append(d.name);
b.append(" (");
b.append(Codec.hexEncode(d.name));
b.append(") \n");
b.append("Size: ");
b.append(d.size);
b.append(" bytes\n");
b.append("First bytes: ");
b.append(Codec.hexEncode(d.bytes));
return b.toString();
}
}

View File

@ -0,0 +1,145 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.poifs.poibrowser;
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.util.*;
/**
* <p>This is a {@link TreeCellRenderer} implementation which is able
* to render arbitrary objects. The {@link ExtendableTreeCellRenderer}
* does not do the rendering itself but instead dispatches to
* class-specific renderers. A class/renderer pair must be registered
* using the {@link #register} method. If a class has no registered
* renderer, the renderer of its closest superclass is used. Since the
* {@link ExtendableTreeCellRenderer} always has a default renderer
* for the {@link Object} class, rendering is always possible. The
* default {@link Object} renderer can be replaced by another renderer
* but it cannot be unregistered.</p>
*
* @author Rainer Klute <a
* href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
*/
public class ExtendableTreeCellRenderer implements TreeCellRenderer
{
/**
* <p>Maps classes to renderers.</p>
*/
protected Map renderers;
public ExtendableTreeCellRenderer()
{
renderers = new HashMap();
register(Object.class, new DefaultTreeCellRenderer()
{
public Component getTreeCellRendererComponent
(JTree tree, Object value, boolean selected,
boolean expanded, boolean leaf, int row, boolean hasFocus)
{
final String s = value.toString();
final JLabel l = new JLabel(s + " ");
if (selected)
{
Util.invert(l);
l.setOpaque(true);
}
return l;
}
});
}
/**
* <p>Registers a renderer for a class.</p>
**/
public void register(final Class c, final TreeCellRenderer renderer)
{
renderers.put(c, renderer);
}
/**
* <p>Unregisters a renderer for a class. The renderer for the
* {@link Object} class cannot be unregistered.</p>
*/
public void unregister(final Class c)
{
if (c == Object.class)
throw new IllegalArgumentException
("Renderer for Object cannot be unregistered.");
renderers.put(c, null);
}
/**
* <p>Renders an object in a tree cell depending of the object's
* class.</p>
*
* @see TreeCellRenderer#getTreeCellRendererComponent
*/
public Component getTreeCellRendererComponent
(final JTree tree, final Object value, final boolean selected,
final boolean expanded, final boolean leaf, final int row,
final boolean hasFocus)
{
final String NULL = "null";
TreeCellRenderer r;
Object userObject;
if (value == null)
userObject = NULL;
else
{
userObject = ((DefaultMutableTreeNode) value).getUserObject();
if (userObject == null)
userObject = NULL;
}
r = findRenderer(userObject.getClass());
return r.getTreeCellRendererComponent
(tree, value, selected, expanded, leaf, row,
hasFocus);
}
/**
* <p>Find the renderer for the specified class.</p>
*/
protected TreeCellRenderer findRenderer(final Class c)
{
final TreeCellRenderer r = (TreeCellRenderer) renderers.get(c);
if (r != null)
/* The class has a renderer. */
return r;
/* The class has no renderer, try the superclass, if any. */
final Class superclass = c.getSuperclass();
if (superclass != null) {
return findRenderer(superclass);
}
return null;
}
}

View File

@ -0,0 +1,132 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.poifs.poibrowser;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
/**
* <p>The main class of the POI Browser. It shows the structure of POI
* filesystems (Microsoft Office documents) in a {@link
* JTree}. Specify their filenames on the command line!</p>
*
* @see POIFSReader
*
* @author Rainer Klute <a
* href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
*/
public class POIBrowser extends JFrame
{
/**
* <p>The tree's root node must be visible to all methods.</p>
*/
protected MutableTreeNode rootNode;
/**
* <p>Takes a bunch of file names as command line parameters,
* opens each of them as a POI filesystem and displays their
* internal structures in a {@link JTree}.</p>
*/
public static void main(String[] args)
{
new POIBrowser().run(args);
}
protected void run(String[] args)
{
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
/* Create the tree model with a root node. The latter is
* invisible but it must be present because a tree model
* always needs a root. */
rootNode = new DefaultMutableTreeNode("POI Filesystems");
DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
/* Create the tree UI element. */
final JTree treeUI = new JTree(treeModel);
getContentPane().add(new JScrollPane(treeUI));
/* Add the POI filesystems to the tree. */
int displayedFiles = 0;
for (int i = 0; i < args.length; i++)
{
final String filename = args[i];
try
{
POIFSReader r = new POIFSReader();
r.registerListener(new TreeReaderListener(filename, rootNode));
r.read(new FileInputStream(filename));
displayedFiles++;
}
catch (IOException ex)
{
System.err.println(filename + ": " + ex);
}
catch (Throwable t)
{
System.err.println("Unexpected exception while reading \"" +
filename + "\":");
t.printStackTrace(System.err);
}
}
/* Exit if there is no file to display (none specified or only
* files with problems). */
if (displayedFiles == 0)
{
System.out.println("No POI filesystem(s) to display.");
System.exit(0);
}
/* Make the tree UI element visible. */
treeUI.setRootVisible(true);
treeUI.setShowsRootHandles(true);
ExtendableTreeCellRenderer etcr = new ExtendableTreeCellRenderer();
etcr.register(DocumentDescriptor.class,
new DocumentDescriptorRenderer());
etcr.register(PropertySetDescriptor.class,
new PropertySetDescriptorRenderer());
treeUI.setCellRenderer(etcr);
setSize(600, 450);
setTitle("POI Browser 0.09");
setVisible(true);
}
}

View File

@ -0,0 +1,77 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.poifs.poibrowser;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.poi.hpsf.MarkUnsupportedException;
import org.apache.poi.hpsf.NoPropertySetStreamException;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.POIFSDocumentPath;
/**
* <p>Describes the most important (whatever that is) features of a
* stream containing a {@link PropertySet}.</p>
*
* @author Rainer Klute (klute@rainer-klute.de)
*/
public class PropertySetDescriptor extends DocumentDescriptor
{
protected PropertySet propertySet;
/**
* <p>Returns this {@link PropertySetDescriptor}'s {@link
* PropertySet}.</p>
*/
public PropertySet getPropertySet()
{
return propertySet;
}
/**
* <p>Creates a {@link PropertySetDescriptor} by reading a {@link
* PropertySet} from a {@link DocumentInputStream}.</p>
*
* @param name The stream's name.
*
* @param path The stream's path in the POI filesystem hierarchy.
*
* @param stream The stream.
*
* @param nrOfBytesToDump The maximum number of bytes to display in a
* dump starting at the beginning of the stream.
*/
public PropertySetDescriptor(final String name,
final POIFSDocumentPath path,
final DocumentInputStream stream,
final int nrOfBytesToDump)
throws NoPropertySetStreamException,
MarkUnsupportedException, UnsupportedEncodingException,
IOException
{
super(name, path, stream, nrOfBytesToDump);
propertySet = PropertySetFactory.create(stream);
}
}

View File

@ -0,0 +1,171 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.poifs.poibrowser;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.Iterator;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import org.apache.poi.hpsf.Property;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.Section;
import org.apache.poi.hpsf.SummaryInformation;
/**
* <p>Renders a {@link PropertySetDescriptor} by more or less dumping
* the stuff into a {@link JTextArea}.</p>
*
* @author Rainer Klute <a
* href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
*/
public class PropertySetDescriptorRenderer extends DocumentDescriptorRenderer
{
public Component getTreeCellRendererComponent(final JTree tree,
final Object value,
final boolean selected,
final boolean expanded,
final boolean leaf,
final int row,
final boolean hasFocus)
{
final PropertySetDescriptor d = (PropertySetDescriptor)
((DefaultMutableTreeNode) value).getUserObject();
final PropertySet ps = d.getPropertySet();
final JPanel p = new JPanel();
final JTextArea text = new JTextArea();
text.setBackground(new Color(200, 255, 200));
text.setFont(new Font("Monospaced", Font.PLAIN, 10));
text.append(renderAsString(d));
text.append("\nByte order: " +
Codec.hexEncode((short) ps.getByteOrder()));
text.append("\nFormat: " +
Codec.hexEncode((short) ps.getFormat()));
text.append("\nOS version: " +
Codec.hexEncode(ps.getOSVersion()));
text.append("\nClass ID: " +
Codec.hexEncode(ps.getClassID()));
text.append("\nSection count: " + ps.getSectionCount());
text.append(sectionsToString(ps.getSections()));
p.add(text);
if (ps instanceof SummaryInformation)
{
/* Use the convenience methods. */
final SummaryInformation si = (SummaryInformation) ps;
text.append("\n");
text.append("\nTitle: " + si.getTitle());
text.append("\nSubject: " + si.getSubject());
text.append("\nAuthor: " + si.getAuthor());
text.append("\nKeywords: " + si.getKeywords());
text.append("\nComments: " + si.getComments());
text.append("\nTemplate: " + si.getTemplate());
text.append("\nLast Author: " + si.getLastAuthor());
text.append("\nRev. Number: " + si.getRevNumber());
text.append("\nEdit Time: " + si.getEditTime());
text.append("\nLast Printed: " + si.getLastPrinted());
text.append("\nCreate Date/Time: " + si.getCreateDateTime());
text.append("\nLast Save Date/Time: " + si.getLastSaveDateTime());
text.append("\nPage Count: " + si.getPageCount());
text.append("\nWord Count: " + si.getWordCount());
text.append("\nChar Count: " + si.getCharCount());
// text.append("\nThumbnail: " + si.getThumbnail());
text.append("\nApplication Name: " + si.getApplicationName());
text.append("\nSecurity: " + si.getSecurity());
}
if (selected)
Util.invert(text);
return p;
}
/**
* <p>Returns a string representation of a list of {@link
* Section}s.</p>
*/
protected String sectionsToString(final List sections)
{
final StringBuffer b = new StringBuffer();
int count = 1;
for (Iterator i = sections.iterator(); i.hasNext();)
{
Section s = (Section) i.next();
String d = toString(s, "Section " + count++);
b.append(d);
}
return b.toString();
}
/**
* <p>Returns a string representation of a {@link Section}.</p>
* @param s the section
* @param name the section's name
* @return a string representation of the {@link Section}
*/
protected String toString(final Section s, final String name)
{
final StringBuffer b = new StringBuffer();
b.append("\n" + name + " Format ID: ");
b.append(Codec.hexEncode(s.getFormatID()));
b.append("\n" + name + " Offset: " + s.getOffset());
b.append("\n" + name + " Section size: " + s.getSize());
b.append("\n" + name + " Property count: " + s.getPropertyCount());
final Property[] properties = s.getProperties();
for (int i = 0; i < properties.length; i++)
{
final Property p = properties[i];
final long id = p.getID();
final long type = p.getType();
final Object value = p.getValue();
b.append('\n');
b.append(name);
b.append(", Name: ");
b.append(id);
b.append(" (");
b.append(s.getPIDString(id));
b.append("), Type: ");
b.append(type);
b.append(", Value: ");
if (value instanceof byte[])
{
byte[] b2 = (byte[]) value;
b.append("0x" + Codec.hexEncode(b2, 0, 4));
b.append(' ');
b.append("0x" + Codec.hexEncode(b2, 4, b2.length - 4));
}
else if (value != null)
b.append(value.toString());
else
b.append("null");
}
return b.toString();
}
}

View File

@ -0,0 +1,219 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.poifs.poibrowser;
import java.util.HashMap;
import java.util.Map;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import org.apache.poi.hpsf.HPSFException;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
import org.apache.poi.poifs.filesystem.DocumentInputStream;
import org.apache.poi.poifs.filesystem.POIFSDocumentPath;
/**
* <p>Organizes document information in a tree model in order to be
* e.g. displayed in a Swing {@link javax.swing.JTree}. An instance of this
* class is created with a root tree node ({@link MutableTreeNode}) and
* registered as a {@link POIFSReaderListener} with a {@link
* org.apache.poi.poifs.eventfilesystem.POIFSReader}. While the latter processes
* a POI filesystem it calls this class' {@link #processPOIFSReaderEvent} for
* each document it has been registered for. This method appends the document it
* processes at the appropriate position into the tree rooted at the
* above mentioned root tree node.</p>
*
* <p>The root tree node should be the root tree node of a {@link
* javax.swing.tree.TreeModel}.</p>
*
* <p>A top-level element in the tree model, i.e. an immediate child
* node of the root node, describes a POI filesystem as such. It is
* suggested to use the file's name (as seen by the operating system)
* but it could be any other string.</p>
*
* <p>The value of a tree node is a {@link DocumentDescriptor}. Unlike
* a {@link org.apache.poi.poifs.filesystem.POIFSDocument} which may be as heavy
* as many megabytes, an instance of {@link DocumentDescriptor} is a
* light-weight object and contains only some meta-information about a
* document.</p>
*
* @author Rainer Klute <a
* href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
*/
public class TreeReaderListener implements POIFSReaderListener
{
/**
* <p>The tree's root node. POI filesystems get attached to this
* node as children.</p>
*/
protected MutableTreeNode rootNode;
/**
* <p>Maps filenames and POI document paths to their associated
* tree nodes.</p>
*/
protected Map pathToNode;
/**
* <p>The name of the file this {@link TreeReaderListener}
* processes. It is used to identify a top-level element in the
* tree. Alternatively any other string can be used. It is just a
* label which should identify a POI filesystem.</p>
*/
protected String filename;
/**
* <p>Creates a {@link TreeReaderListener} which should then be
* registered with a
* {@link org.apache.poi.poifs.eventfilesystem.POIFSReader}.</p>
*
* @param filename The name of the POI filesystem, i.e. the name
* of the file the POI filesystem resides in. Alternatively any
* other string can be used.
*
* @param rootNode All document information will be attached as
* descendands to this tree node.
*/
public TreeReaderListener(final String filename,
final MutableTreeNode rootNode)
{
this.filename = filename;
this.rootNode = rootNode;
pathToNode = new HashMap(15); // Should be a reasonable guess.
}
/** <p>The number of bytes to dump.</p> */
private int nrOfBytes = 50;
public void setNrOfBytes(final int nrOfBytes)
{
this.nrOfBytes = nrOfBytes;
}
public int getNrOfBytes()
{
return nrOfBytes;
}
/**
* <p>A document in the POI filesystem has been opened for
* reading. This method retrieves properties of the document and
* adds them to a tree model.</p>
*/
public void processPOIFSReaderEvent(final POIFSReaderEvent event)
{
DocumentDescriptor d;
final DocumentInputStream is = event.getStream();
if (!is.markSupported())
throw new UnsupportedOperationException(is.getClass().getName() +
" does not support mark().");
/* Try do handle this document as a property set. We receive
* an exception if is no property set and handle it as a
* document of some other format. We are not concerned about
* that document's details. */
try
{
d = new PropertySetDescriptor(event.getName(), event.getPath(),
is, nrOfBytes);
}
catch (HPSFException ex)
{
d = new DocumentDescriptor(event.getName(), event.getPath(),
is, nrOfBytes);
}
catch (Throwable t)
{
System.err.println
("Unexpected exception while processing " +
event.getName() + " in " + event.getPath().toString());
t.printStackTrace(System.err);
throw new RuntimeException(t.getMessage());
}
is.close();
final MutableTreeNode parentNode = getNode(d.path, filename, rootNode);
final MutableTreeNode nameNode = new DefaultMutableTreeNode(d.name);
parentNode.insert(nameNode, 0);
final MutableTreeNode dNode = new DefaultMutableTreeNode(d);
nameNode.insert(dNode, 0);
}
/**
* <p>Locates the parent node for a document entry in the tree
* model. If the parent node does not yet exist it will be
* created, too. This is done recursively, if needed.</p>
*
* @param path The tree node for this path is located.
*
* @param fsName The name of the POI filesystem. This is just a
* string which is displayed in the tree at the top lovel.
*
* @param root The root node.
*/
private MutableTreeNode getNode(final POIFSDocumentPath path,
final String fsName,
final MutableTreeNode root)
{
MutableTreeNode n = (MutableTreeNode) pathToNode.get(path);
if (n != null)
/* Node found in map, just return it. */
return n;
if (path.length() == 0)
{
/* This is the root path of the POI filesystem. Its tree
* node is resp. must be located below the tree node of
* the POI filesystem itself. This is a tree node with the
* POI filesystem's name (this the operating system file's
* name) as its key it the path-to-node map. */
n = (MutableTreeNode) pathToNode.get(fsName);
if (n == null)
{
/* A tree node for the POI filesystem does not yet
* exist. */
n = new DefaultMutableTreeNode(fsName);
pathToNode.put(fsName, n);
root.insert(n, 0);
}
return n;
}
/* else - The path is somewhere down in the POI filesystem's
* hierarchy. We need the tree node of this path's parent
* and attach our new node to it. */
final String name = path.getComponent(path.length() - 1);
final POIFSDocumentPath parentPath = path.getParent();
final MutableTreeNode parentNode =
getNode(parentPath, fsName, root);
n = new DefaultMutableTreeNode(name);
pathToNode.put(path, n);
parentNode.insert(n, 0);
return n;
}
}

View File

@ -0,0 +1,44 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.poifs.poibrowser;
import java.awt.*;
import javax.swing.*;
/**
* <p>Contains various (well, just one at the moment) static utility
* methods.</p>
*
* @author Rainer Klute <a
* href="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
*/
public class Util {
/**
* <p>Makes a Swing component inverted by swapping its foreground
* and background colors. Hint: Depending on your needs it might
* also be a good idea to call <tt>c.setOpaque(true)</tt>.</p>
*/
public static void invert(JComponent c) {
Color invBackground = c.getForeground();
Color invForeground = c.getBackground();
c.setBackground(invBackground);
c.setForeground(invForeground);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,72 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.ss.examples;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
/**
* Shows how various alignment options work.
*/
public class AligningCells {
public static void main(String[] args) throws IOException {
Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createRow((short) 2);
row.setHeightInPoints(30);
for (int i = 0; i < 8; i++) {
//column width is set in units of 1/256th of a character width
sheet.setColumnWidth(i, 256 * 15);
}
createCell(wb, row, (short) 0, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_BOTTOM);
createCell(wb, row, (short) 1, CellStyle.ALIGN_CENTER_SELECTION, CellStyle.VERTICAL_BOTTOM);
createCell(wb, row, (short) 2, CellStyle.ALIGN_FILL, CellStyle.VERTICAL_CENTER);
createCell(wb, row, (short) 3, CellStyle.ALIGN_GENERAL, CellStyle.VERTICAL_CENTER);
createCell(wb, row, (short) 4, CellStyle.ALIGN_JUSTIFY, CellStyle.VERTICAL_JUSTIFY);
createCell(wb, row, (short) 5, CellStyle.ALIGN_LEFT, CellStyle.VERTICAL_TOP);
createCell(wb, row, (short) 6, CellStyle.ALIGN_RIGHT, CellStyle.VERTICAL_TOP);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("ss-example-align.xlsx");
wb.write(fileOut);
fileOut.close();
}
/**
* Creates a cell and aligns it a certain way.
*
* @param wb the workbook
* @param row the row to create the cell in
* @param column the column number to create the cell in
* @param halign the horizontal alignment for the cell.
*/
private static void createCell(Workbook wb, Row row, short column, short halign, short valign) {
CreationHelper ch = wb.getCreationHelper();
Cell cell = row.createCell(column);
cell.setCellValue(ch.createRichTextString("Align It"));
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(halign);
cellStyle.setVerticalAlignment(valign);
cell.setCellStyle(cellStyle);
}
}

View File

@ -0,0 +1,325 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.ss.examples;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.util.Map;
import java.util.HashMap;
import java.util.Calendar;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
/**
* A business plan demo
* Usage:
* BusinessPlan -xls|xlsx
*
* @author Yegor Kozlov
*/
public class BusinessPlan {
private static SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM");
private static final String[] titles = {
"ID", "Project Name", "Owner", "Days", "Start", "End"};
//sample data to fill the sheet.
private static final String[][] data = {
{"1.0", "Marketing Research Tactical Plan", "J. Dow", "70", "9-Jul", null,
"x", "x", "x", "x", "x", "x", "x", "x", "x", "x", "x"},
null,
{"1.1", "Scope Definition Phase", "J. Dow", "10", "9-Jul", null,
"x", "x", null, null, null, null, null, null, null, null, null},
{"1.1.1", "Define research objectives", "J. Dow", "3", "9-Jul", null,
"x", null, null, null, null, null, null, null, null, null, null},
{"1.1.2", "Define research requirements", "S. Jones", "7", "10-Jul", null,
"x", "x", null, null, null, null, null, null, null, null, null},
{"1.1.3", "Determine in-house resource or hire vendor", "J. Dow", "2", "15-Jul", null,
"x", "x", null, null, null, null, null, null, null, null, null},
null,
{"1.2", "Vendor Selection Phase", "J. Dow", "19", "19-Jul", null,
null, "x", "x", "x", "x", null, null, null, null, null, null},
{"1.2.1", "Define vendor selection criteria", "J. Dow", "3", "19-Jul", null,
null, "x", null, null, null, null, null, null, null, null, null},
{"1.2.2", "Develop vendor selection questionnaire", "S. Jones, T. Wates", "2", "22-Jul", null,
null, "x", "x", null, null, null, null, null, null, null, null},
{"1.2.3", "Develop Statement of Work", "S. Jones", "4", "26-Jul", null,
null, null, "x", "x", null, null, null, null, null, null, null},
{"1.2.4", "Evaluate proposal", "J. Dow, S. Jones", "4", "2-Aug", null,
null, null, null, "x", "x", null, null, null, null, null, null},
{"1.2.5", "Select vendor", "J. Dow", "1", "6-Aug", null,
null, null, null, null, "x", null, null, null, null, null, null},
null,
{"1.3", "Research Phase", "G. Lee", "47", "9-Aug", null,
null, null, null, null, "x", "x", "x", "x", "x", "x", "x"},
{"1.3.1", "Develop market research information needs questionnaire", "G. Lee", "2", "9-Aug", null,
null, null, null, null, "x", null, null, null, null, null, null},
{"1.3.2", "Interview marketing group for market research needs", "G. Lee", "2", "11-Aug", null,
null, null, null, null, "x", "x", null, null, null, null, null},
{"1.3.3", "Document information needs", "G. Lee, S. Jones", "1", "13-Aug", null,
null, null, null, null, null, "x", null, null, null, null, null},
};
public static void main(String[] args) throws Exception {
Workbook wb;
if(args.length > 0 && args[0].equals("-xls")) wb = new HSSFWorkbook();
else wb = new XSSFWorkbook();
Map<String, CellStyle> styles = createStyles(wb);
Sheet sheet = wb.createSheet("Business Plan");
//turn off gridlines
sheet.setDisplayGridlines(false);
sheet.setPrintGridlines(false);
sheet.setFitToPage(true);
sheet.setHorizontallyCenter(true);
PrintSetup printSetup = sheet.getPrintSetup();
printSetup.setLandscape(true);
//the following three statements are required only for HSSF
sheet.setAutobreaks(true);
printSetup.setFitHeight((short)1);
printSetup.setFitWidth((short)1);
//the header row: centered text in 48pt font
Row headerRow = sheet.createRow(0);
headerRow.setHeightInPoints(12.75f);
for (int i = 0; i < titles.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(titles[i]);
cell.setCellStyle(styles.get("header"));
}
//columns for 11 weeks starting from 9-Jul
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
calendar.setTime(fmt.parse("9-Jul"));
calendar.set(Calendar.YEAR, year);
for (int i = 0; i < 11; i++) {
Cell cell = headerRow.createCell(titles.length + i);
cell.setCellValue(calendar);
cell.setCellStyle(styles.get("header_date"));
calendar.roll(Calendar.WEEK_OF_YEAR, true);
}
//freeze the first row
sheet.createFreezePane(0, 1);
Row row;
Cell cell;
int rownum = 1;
for (int i = 0; i < data.length; i++, rownum++) {
row = sheet.createRow(rownum);
if(data[i] == null) continue;
for (int j = 0; j < data[i].length; j++) {
cell = row.createCell(j);
String styleName;
boolean isHeader = i == 0 || data[i-1] == null;
switch(j){
case 0:
if(isHeader) {
styleName = "cell_b";
cell.setCellValue(Double.parseDouble(data[i][j]));
} else {
styleName = "cell_normal";
cell.setCellValue(data[i][j]);
}
break;
case 1:
if(isHeader) {
styleName = i == 0 ? "cell_h" : "cell_bb";
} else {
styleName = "cell_indented";
}
cell.setCellValue(data[i][j]);
break;
case 2:
styleName = isHeader ? "cell_b" : "cell_normal";
cell.setCellValue(data[i][j]);
break;
case 3:
styleName = isHeader ? "cell_b_centered" : "cell_normal_centered";
cell.setCellValue(Integer.parseInt(data[i][j]));
break;
case 4: {
calendar.setTime(fmt.parse(data[i][j]));
calendar.set(Calendar.YEAR, year);
cell.setCellValue(calendar);
styleName = isHeader ? "cell_b_date" : "cell_normal_date";
break;
}
case 5: {
int r = rownum + 1;
String fmla = "IF(AND(D"+r+",E"+r+"),E"+r+"+D"+r+",\"\")";
cell.setCellFormula(fmla);
styleName = isHeader ? "cell_bg" : "cell_g";
break;
}
default:
styleName = data[i][j] != null ? "cell_blue" : "cell_normal";
}
cell.setCellStyle(styles.get(styleName));
}
}
//group rows for each phase, row numbers are 0-based
sheet.groupRow(4, 6);
sheet.groupRow(9, 13);
sheet.groupRow(16, 18);
//set column widths, the width is measured in units of 1/256th of a character width
sheet.setColumnWidth(0, 256*6);
sheet.setColumnWidth(1, 256*33);
sheet.setColumnWidth(2, 256*20);
sheet.setZoom(3, 4);
// Write the output to a file
String file = "businessplan.xls";
if(wb instanceof XSSFWorkbook) file += "x";
FileOutputStream out = new FileOutputStream(file);
wb.write(out);
out.close();
}
/**
* create a library of cell styles
*/
private static Map<String, CellStyle> createStyles(Workbook wb){
Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
DataFormat df = wb.createDataFormat();
CellStyle style;
Font headerFont = wb.createFont();
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFont(headerFont);
styles.put("header", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFont(headerFont);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("header_date", style);
Font font1 = wb.createFont();
font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setFont(font1);
styles.put("cell_b", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFont(font1);
styles.put("cell_b_centered", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setFont(font1);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_b_date", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setFont(font1);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_g", style);
Font font2 = wb.createFont();
font2.setColor(IndexedColors.BLUE.getIndex());
font2.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setFont(font2);
styles.put("cell_bb", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setFont(font1);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_bg", style);
Font font3 = wb.createFont();
font3.setFontHeightInPoints((short)14);
font3.setColor(IndexedColors.DARK_BLUE.getIndex());
font3.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setFont(font3);
style.setWrapText(true);
styles.put("cell_h", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setWrapText(true);
styles.put("cell_normal", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setWrapText(true);
styles.put("cell_normal_centered", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setWrapText(true);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_normal_date", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setIndention((short)1);
style.setWrapText(true);
styles.put("cell_indented", style);
style = createBorderedStyle(wb);
style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
styles.put("cell_blue", style);
return styles;
}
private static CellStyle createBorderedStyle(Workbook wb){
CellStyle style = wb.createCellStyle();
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
return style;
}
}

Some files were not shown because too many files have changed in this diff Show More