为白痴自动更正 Excel 电子表格

发布于 2024-07-30 08:34:57 字数 669 浏览 1 评论 0原文

我有一个客户无法正确格式化电子表格。 我已经十次要求他们遵循合理的指导方针(如用正确的数据类型格式化列),但由于他们缺乏智慧或专业精神或两者兼而有之,他们就是不能/不会这样做。

我为电子表格设置了 ODBC 连接,并提取除包含产品计数或产品价格的列之外的数据。 除非我在 Excel 中手动将每一列的类型设置为“数字”,否则这些值在显示结果的 ASP 页面中显示为空。

是否可以编写一条 SQL 语句来改变传入的数据类型? 我不在乎是否将每一列转换为字符串,只要 ADODB.Recordset 将显示所有列的值即可。

代码:

<%
Dim MM_SQLSource

MM_SQLSource = "SELECT * FROM [Inventory$]"
Set rsGetExcelInfo = Server.CreateObject("ADODB.Recordset")
rsGetExcelInfo.ActiveConnection = MM_Excel_Connect
rsGetExcelInfo.Source = MM_SQLSource
rsGetExcelInfo.CursorType = 0
rsGetExcelInfo.CursorLocation = 2
rsGetExcelInfo.LockType = 1

rsGetExcelInfo.Open()
%>

I have a client who is incapable of formatting spreadsheets properly. I've asked them 10 times over to follow a sensible guideline (As in FORMAT THE COLUMNS WITH THE CORRECT DATATYPE) but in their lack of intelligence or professionalism or both, they just can't /won't do it.

I have an ODBC connection set up for the spreadsheets and pulls the data except for the columns that contain product counts or product prices. Unless I manually set the type for each of those columns to "Number" in Excel, the values appear null in the ASP page that displays the results.

Is it possible to write a SQL statement that will alter the datatype as it comes in? I don't care it if converts every column to a string as long as the ADODB.Recordset will display the values of all of the columns.

Code:

<%
Dim MM_SQLSource

MM_SQLSource = "SELECT * FROM [Inventory$]"
Set rsGetExcelInfo = Server.CreateObject("ADODB.Recordset")
rsGetExcelInfo.ActiveConnection = MM_Excel_Connect
rsGetExcelInfo.Source = MM_SQLSource
rsGetExcelInfo.CursorType = 0
rsGetExcelInfo.CursorLocation = 2
rsGetExcelInfo.LockType = 1

rsGetExcelInfo.Open()
%>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

轮廓§ 2024-08-06 08:34:58

这可能不完全是你客户的错。 Excel 有时对于如何设置列格式有自己的想法。 例如,如果您有一列包含邮政编码,其中一些带有 Plus 4,另一些则没有,那么该列的格式几乎是一团糟。

至于你原来的问题,根据这个网站,CONVERT是一个有效的 SQL 标量函数,所以也许类似的东西

SELECT CONVERT(BadField, SQL_CHAR) AS FixedField FROM [Inventory$]

可以工作?

我的第一反应是建议使用 COM 从电子表格中读取数据。 我很确定您能够读取每个单元格的格式并进行相应的处理,但我总是发现通过 COM 使用 Excel 很困难,而且速度不是很快(我只用 C++ 完成过)。

It may not be entirely your client's fault. Excel sometimes has its own ideas about how a column should be formatted. For example, if you have a column containing zip codes, some with the Plus 4, others without, it is pretty much a crap shoot as to how that column will be formatted.

As for your original question, according to this site, CONVERT is a valid SQL scalar function, so maybe something like

SELECT CONVERT(BadField, SQL_CHAR) AS FixedField FROM [Inventory$]

might work?

My first inclination was to suggest using COM to read the data from the spreadsheet. I'm pretty sure you would be able to read each cell's format and deal with it accordingly, but I always found Excel via COM to be difficult and not terribly fast (I've only done it from C++).

你与清晨阳光 2024-08-06 08:34:58

不幸的是,据我所知,所有 ODBC/ADO/OLEDB/任何小工具在检查几行后都会猜测每列的数据类型(我相信默认值为 8)。

有适用于 Python[1]/Perl/PHP/.Net 等的包/模块/库,可让您逐个单元格地读取 Excel 文件。 更好的将为每个单元格提供一个值和一个细粒度的类型(文本/数字/日期/布尔/错误/空)——有了这些信息以及每列中应该包含什么内容的知识,您可以做修理工作。

[1] 例如http://pypi.python.org/pypi/xlrd,我是其中的作者......在被(a)ODBC问题(b)“保存”困扰之后-as-CSV' 问题 (c) COM 问题

Unfortunately AFAIK all the ODBC/ADO/OLEDB/whatever gadgets guess the data type for each column after inspecting a few rows (default is 8, I believe).

There are packages/modules/libraries available for Python[1]/Perl/PHP/.Net etc that will let you read an Excel file cell by cell. The better ones will give you both a value and a finely-grained type (text/numeric/date/boolean/error/empty) for each cell -- armed with that info plus a knowledge of what should be in each column, you can do a repair job.

[1] E.g. http://pypi.python.org/pypi/xlrd of which I'm the author ... after being bitten by (a) the ODBC problem (b) the 'save-as-CSV' problem (c) the COM problem

小糖芽 2024-08-06 08:34:58

停止使用 Excel 并将其放入真正的数据库中,并使用一些表格来输入用户数据怎么样?

将电子表格放入数据库应该不会太困难。 访问就可以了。 或者使用 SQL Server 后端和 Access 前端。 或者,如果您可以付出更多努力,则可以制作一个网络前端。

您会发现,执行此操作越困难,您的数据就越糟糕,并且错误、不一致和遗漏也就越多。

帮大家一个忙,让这件事正式化。

How about stop using Excel and put it into a real database with some forms for user data entry?

It shouldn't be too overly difficult to put the spreadsheet into a database. Access would do fine. Or use a SQL Server back end and an Access front-end. Or if you can handle more effort, make a web front end.

What you'll find is that the more difficult it is to do this, the worse your data was anyway, and the more errors, inconsistencies, and omissions you had.

Do everyone a favor and formalize this thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文