Return-Path: X-Original-To: apmail-poi-user-archive@www.apache.org Delivered-To: apmail-poi-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 00F9718241 for ; Wed, 9 Mar 2016 17:47:53 +0000 (UTC) Received: (qmail 75112 invoked by uid 500); 9 Mar 2016 17:47:52 -0000 Delivered-To: apmail-poi-user-archive@poi.apache.org Received: (qmail 75088 invoked by uid 500); 9 Mar 2016 17:47:52 -0000 Mailing-List: contact user-help@poi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "POI Users List" Delivered-To: mailing list user@poi.apache.org Received: (qmail 75077 invoked by uid 99); 9 Mar 2016 17:47:52 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 09 Mar 2016 17:47:52 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id B8F17C366D for ; Wed, 9 Mar 2016 17:47:51 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 2 X-Spam-Level: ** X-Spam-Status: No, score=2 tagged_above=-999 required=6.31 tests=[HTML_MESSAGE=2] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id hk7jjnvuPpMj for ; Wed, 9 Mar 2016 17:47:49 +0000 (UTC) Received: from mx0a-00153b01.pphosted.com (mx0a-00153b01.pphosted.com [67.231.145.92]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTPS id A0D3A5F640 for ; Wed, 9 Mar 2016 17:47:48 +0000 (UTC) Received: from pps.filterd (m0043378.ppops.net [127.0.0.1]) by mx0a-00153b01.pphosted.com (8.15.0.59/8.15.0.59) with SMTP id u29Hke4R000695 for ; Wed, 9 Mar 2016 09:47:41 -0800 Received: from mail-pf0-f180.google.com (mail-pf0-f180.google.com [209.85.192.180]) by mx0a-00153b01.pphosted.com with ESMTP id 21j4dmapkf-1 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 09 Mar 2016 09:47:41 -0800 Received: by mail-pf0-f180.google.com with SMTP id u190so16919553pfb.3 for ; Wed, 09 Mar 2016 09:47:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to; bh=yoXvuhmFWQBGDgD/o4FypZYueO5UePFeYMm3R9ayqJo=; b=KJUD2bqDI8IZ8SvD2ETvQkdm36lb6SJsGIAgBpu7sEnCiCuVlSNIDRQFKh2t/2ftFI vsYuyEOAHj4VMXAgiwtAqsErwXxdZ45wQVOo4i25QxXs/YhMuLsEcTCLo6p24Gb7i1+Q Iua+iY0y6QkmSUsicuA7mHKzp7SnV6Kf3kpXpfTOh2etJCpxJqur4kf/mbI4AeJxHCs2 Ga+oqI6fjyanP+6bUS2EGy/AF1LTuqj5KLklFavVUjfmdDfG+9Cts4I1MsWYLC3lvcUe ohm+qrBwh9j405qLk1YCK9OqRMOEpuAUfbhcV2vzMZ2prgANzm9/uyz3hvbYkkmnYJuU BVnw== X-Gm-Message-State: AD7BkJKfiSGkuwnSGvgdeju57O7LZuFA8TTRUOjYLddGuWPSsHY0AuF04jaL5AGM908BUm8DwNA1EiiM74b1NaSk6z8dBCjyZ2rYeWn/HiK5T0Yc//In7JHFQWq7UuyArdDio/gj2EDoWNGOxXK7DpUuAks4Zb3pnOiLxWPtk+7ziXGz X-Received: by 10.98.13.132 with SMTP id 4mr19468253pfn.122.1457545660391; Wed, 09 Mar 2016 09:47:40 -0800 (PST) MIME-Version: 1.0 X-Received: by 10.98.13.132 with SMTP id 4mr19468207pfn.122.1457545659933; Wed, 09 Mar 2016 09:47:39 -0800 (PST) Received: by 10.66.161.41 with HTTP; Wed, 9 Mar 2016 09:47:39 -0800 (PST) In-Reply-To: References: Date: Wed, 9 Mar 2016 09:47:39 -0800 Message-ID: Subject: Re: Detect if a cell is date formatted From: Blake Watson To: POI Users List Content-Type: multipart/alternative; boundary=001a11431b784dfe23052da14ba3 X-Proofpoint-Spam-Reason: safe X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2016-03-09_16:,, signatures=0 --001a11431b784dfe23052da14ba3 Content-Type: text/plain; charset=UTF-8 That's cool! I'd be concerned about whether it would work in a situation where a column has been defined as a numeric one (i.e., so the cells could easily be nil), but I hadn't thought of using "IsADateFormat" before... On Wed, Mar 9, 2016 at 1:16 AM, Bengt Rodehav wrote: > Thanks for your reply Blake, > > Yeah I was actually considering this (put a number in, check if date, then > clear the cell) but I instead did this: > > I copied the following method from DateUtil and removed the check against > the value of the cell. > > public static boolean isCellDateFormatted(Cell cell) { > if (cell == null) return false; > boolean bDate = false; > > // double d = cell.getNumericCellValue(); > // if ( DateUtil.isValidExcelDate(d) ) { > CellStyle style = cell.getCellStyle(); > if(style==null) return false; > int i = style.getDataFormat(); > String f = style.getDataFormatString(); > bDate = DateUtil.isADateFormat(i, f); > // } > return bDate; > } > > Now only the actual format is checked, not the value. I would prefer not > having to duplicate and maintain this code but I think it's still better > than having to put temporary values in cells. I think the method is not > properly named since it doesn't only check the formatting but also throws > in a check concerning the cell value. I think it would be wise to add an > extra parameter indicating whether the cell's value should be checked as > well or if it is just a format check. The old version (with just the Cell > parameter) could still exist but call the new method with a second > parameter indicating that the cell's value should be checked. > > /Bengt > > 2016-03-07 18:53 GMT+01:00 Blake Watson : > > > It may not be possible, in the sense that Excel may not decide that a > cell > > is a date unless it has a numeric value in it and is so formatted. I've > had > > a lot of what seem to be odd phenomena around that, which make sense if > you > > consider how spreadsheets evolved and are commonly used. > > > > I realize it's an extra step but if you can't without putting a number > in, > > why not put a number in? > > > > On Mon, Mar 7, 2016 at 7:52 AM, Bengt Rodehav wrote: > > > > > I have a situation where an Excel template I get from a third party > (this > > > is for EIOPA's Solvency II reporting) wrongly has set date formatting > in > > a > > > number of cells. I want to fix this programatically. > > > > > > I use code like this to try to detect date formatting: > > > > > > if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { > > > if (DateUtil.isCellDateFormatted(cell)) { > > > System.out.println(" Date at sheet: " + sheetName + ", row: " + > > > row.getRowNum() + ", column: " + cell.getColumnIndex()); > > > } > > > } > > > > > > However, when the cells are empty (remember this is a template), the > cell > > > type always seems to be Cell.CELL_TYPE_STRING not > Cell.CELL_TYPE_NUMERIC > > so > > > the above code doesn't find any date cells. > > > > > > If I change the code to... > > > > > > if (cell.getCellType() == Cell.CELL_TYPE_STRING) { > > > if (DateUtil.isCellDateFormatted(cell)) { > > > System.out.println(" Date at sheet: " + sheetName + ", row: " + > > > row.getRowNum() + ", column: " + cell.getColumnIndex()); > > > } > > > } > > > > > > ...then I get an exceptions since the DateUtil.isCellDateFormatted() > > > requires the cell to contain a numerice value. > > > > > > So, I conclude that the cell's type seems to be determined by the value > > in > > > the cell - not the metadata for the cell. This seems a bit strange to > me. > > > The problem is that if the user enters a numeric value in the cell, > then > > it > > > will be date formatted (which it shouldn't). The user is allowed to > enter > > > any string (even a numeric value). So, I want to remove the date > > formatting > > > regardless of the cell type. But I cannot detect that it is a date > > unless I > > > enter a numeric value in the cell. > > > > > > Does anyone have a tip as to how I can detect date formatting without > > > requiring a value in the cell? > > > > > > /Bengt > > > > > > > > > > > -- > > > > *Blake Watson* > > > > *PNMAC* > > Application Development Manager > > 5898 Condor Drive > > Moorpark, CA 93021 > > (805) 330.4911 x7742 > > blake.watson@pnmac.com > > www.PennyMacUSA.com > > > -- *Blake Watson* *PNMAC* Application Development Manager 5898 Condor Drive Moorpark, CA 93021 (805) 330.4911 x7742 blake.watson@pnmac.com www.PennyMacUSA.com --001a11431b784dfe23052da14ba3--