使用Excel的库 - jxl?(Library for working with Excel - jxl?)
jxl的可靠性和强大性如何? 我正在尝试从Java创建一个excel文件,执行合并单元格,创建图形,设置不同背景颜色(带渐变)等操作 - 基于我从用户收集的信息。 另外,我想用当前日期命名工作表并设置访问权限(只读或可编辑)。 我可以使用jxl完成所有这些吗?
How reliable and powerful is jxl? I'm trying to create an excel file from Java, performing operations like merging cells, creating graphs, setting different background colours (with gradients) - basing on information I collect from the user. Also, I would like to name sheets with current date and set access rights (read-only or editable). Can I do all this using jxl?
原文:https://stackoverflow.com/questions/9575810
最满意答案
我个人更喜欢这样的:
def self.parse(arg) key, value = arg.to_s.split(':') { 'nil' => new(nil), 'string' => new(value), 'boolean' => new(value == 'true'), 'symbol' => new(value.to_sym), 'number' => new(value.include?('.') ? BigDecimal(value) : Integer(value)) }[key] end
上面的代码实际上是2行,为了可读性分成多行。 但是,如果使用
case
是必须的,那么您可以将您的代码更改为:def self.parse(arg) key, value = arg.to_s.split(':') case key when 'nil' then new(nil) when 'string' then new(value) when 'boolean' then new(value == 'true') when 'symbol' then new(value.to_sym) when 'number' then new(value.include?('.') ? BigDecimal(value) : Integer(value)) end end
I would personally prefer this:
def self.parse(arg) key, value = arg.to_s.split(':') { 'nil' => new(nil), 'string' => new(value), 'boolean' => new(value == 'true'), 'symbol' => new(value.to_sym), 'number' => new(value.include?('.') ? BigDecimal(value) : Integer(value)) }[key] end
Code above is actually of 2 lines, broken into multiple lines for readability sake. However, if using
case
is a must then you can change your code to this:def self.parse(arg) key, value = arg.to_s.split(':') case key when 'nil' then new(nil) when 'string' then new(value) when 'boolean' then new(value == 'true') when 'symbol' then new(value.to_sym) when 'number' then new(value.include?('.') ? BigDecimal(value) : Integer(value)) end end
相关问答
更多-
Midwire运行了一些基准测试并得出结论,如果/ elsif比情况更快,因为“隐式地使用更昂贵的===运算符进行比较”。 这是我得到这个报价的地方。 它比较/ elsif语句与case 。 这是非常彻底的,并探讨指令序列的差异,肯定会给你一个更好的想法。 我从帖子中拉出的主要内容是,如果if和else两者没有巨大差异,两者通常可以互换使用。 取决于您拥有多少个案例,可能会出现一些重大差异。 n = 1 (last clause matches) if: 7.4821e-07 three ...
-
根据Ruby文档: case语句由一个可选条件组成,该条件位于case的参数位置,并且在子句时为零或更多。 匹配条件的第一个when子句(或者,如果条件为null,则评估为布尔真值)“wins”,并执行其代码节。 如果指定current_user作为大小写条件,那么将执行第一个匹配current_user表达式。 current_user.is_admin? 返回一个永远不会等于current_user的布尔值,所以你的第二个例子将总是采用else分支: case current_user when n ...
-
Ruby案例比较(Ruby Case Comparison)[2022-11-21]
当执行你的valid_handles查询时,这个结果是一个数组数组。 即使您只选择了一个列( SELECT twitter_handle FROM... ), valid_handles_results数组中的条目也是包含单个元素的数组。 所以要修复错误,用valid_handle[0].casecmp(record[1]) 虽然这看起来有点令人困惑,但背后的原因是这意味着@userDb.execute可以返回一致的结构,而不管查询中选择的列的数量是多少。 为了帮助调试将来的类似错误,了解“未定义的方法”错 ... -
我假定系统或最终用户在某个时刻有效地选择了文本类型,并且需要将其转换为要使用的类。 否则,您可以编写简单地引用并实例化正确类的调用代码。 你可以通过定义符号type和类之间的映射来使你更清晰。 所以你可以在create_account的范围内做到这一点: ACCOUNT_CLASS_FOR = Hash[ current: CurrentAccount, savings: SavingsAccount, business: BusinessAccount, ir: IRAcc ...
-
Ruby的case语句比大多数其他switch语句更灵活。 它使用===运算符,而不是==运算符。 类按照行定义===运算符 def ===(other)other.is_a? 自我是自己的终极目标 所以,你真正想要的是: def case_method case self when User do_something_with_user when SomeOtherClass do_something_else end # else is un-needed as it wi ...
-
我个人更喜欢这样的: def self.parse(arg) key, value = arg.to_s.split(':') { 'nil' => new(nil), 'string' => new(value), 'boolean' => new(value == 'true'), 'symbol' => new(value.to_sym), 'number' => new(value.include?('.') ? BigDecimal( ...
-
Ruby - 方法工厂(Ruby - Method Factory)[2021-11-30]
我应该如何在这个例子中实现Factory Method? 你不应该。 在基于类的编程中,工厂方法模式是一种创建模式,它使用工厂方法来处理创建对象的问题,而无需指定将要创建的对象的确切类。 如果你有专门的Name子类,那么也许吧。 我的思维过程应该如何才能使用这种设计模式? 你的第一个想法应该是“这种模式在这里是否有意义?我是否理解这种模式足以在这里尝试应用它?”。 如果答案是“是”和“是”,那么你就应用它。 更新: 你最有可能在这里使用的是工厂模式 。 在基于类的编程中,工厂是类的构造函数的抽象 class ... -
从Python到Ruby(From Python to Ruby)[2022-07-04]
2.2.2 :001 > require 'set' => true 2.2.2 :002 > [1, 2, "test"].join ... -
Ruby条件/案例表达(Ruby Conditionals/Case Expression)[2023-08-19]
你写错了你的case陈述。 它有两种形式,与其他语言相比是不寻常的。 第一种形式采用参数,并将该参数与所有可能的情况进行比较。 第二种形式没有参数,每个案例都是独立评估的。 最小的修复是这样的: def odd_or_even(string) case when string.length.even? then "even" when string.length.odd? then "odd" end end 这是因为在使用参数“Ruby”调用时,您的代码看起来像这样: def odd_o ... -
如果消息包含5个或更多大写单词,则以下内容将返回true。 def is_message_shouting?(message) shouted_words = 0 message.split(' ').each do |word| shouted_words += 1 if word.upcase == word end shouted_words >= 5 end puts is_message_shouting? 'THIS IS A VERY SHOUTY MESSAGE ...