有一个文件夹里边时以日期命名的excel,字段名都一样,我需要在每个excel里面加一个列,获取到当前文件名,并加到文件中
在 Kettle 中可以使用 "Get File Names" 步骤获取指定文件夹下的文件名,然后使用 "Excel Input" 步骤读取 Excel 文件中的数据,再使用 "Add Constants" 步骤添加一个常量列,将文件名作为常量添加到 Excel 数据中。
1.使用 "Get File Names" 步骤获取指定文件夹下的文件名
选择 "Get File Names" 步骤,配置 "File / Directory" 为指定的文件夹路径,"File Filters" 为 "*.xlsx"(或者是其他你需要的 Excel 文件格式),"Fields" 中可以勾选 "Filename",这样就会获取到文件名,存储在一个名为 "filename" 的字段中。
2.使用 "Excel Input" 步骤读取 Excel 文件中的数据
选择 "Excel Input" 步骤,配置 "Filename from field" 为上一步获取到的 "filename" 字段,"Sheet name(s)" 为需要读取的工作表名称,"Fields" 中添加需要读取的字段。
🆗
3.使用 "Add Constants" 步骤添加一个常量列选择 "Add Constants" 步骤,添加一个常量列,将 "filename" 字段作为常量值添加到 Excel 数据中。
4.输出数据选择 "Text File Output" 步骤,将处理后的 Excel 数据输出到指定的文件中。
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下。
只在 Kettle 中,您可以使用以下步骤实现你的上述功能:
使用 "Get File Names" 步骤获取指定文件夹下的所有 Excel 文件名,并将文件名保存到 "ExcelFileName" 变量中。具体步骤如下:
使用 "Excel Input" 步骤读取每个 Excel 文件,并将文件名作为一个字段添加到输出流中。具体步骤如下:
使用 "Add Constants" 或 "Calculator" 步骤创建一个新的列 "FileName",并将 "ExcelFileName" 变量的值赋值给该列。具体步骤如下:
使用 "Microsoft Excel Output" 步骤将带有文件名的 Excel 文件输出到目标文件夹中。具体步骤如下:
希望这些步骤能够帮助您解决问题。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢
kettle常用操作--Excel文件和数据库表之间的数据转换
可以借鉴下
https://blog.csdn.net/qq_44749491/article/details/126786955
以下答案由GPT-3.5大模型与博主波罗歌共同编写:
在Kettle中,获取文件名可以使用“Get file names”步骤,可以配置文件夹路径、文件名过滤规则等参数,获取到需要处理的文件名。
接着可以使用“Excel input”步骤读取excel文件,然后使用“Add constants”步骤添加一个常量列,常量值为当前文件名,添加到读取的excel数据中。
具体操作步骤如下:
添加“Get file names”步骤,配置好文件夹路径和文件名过滤规则等参数。将获取到的文件名输出到流中。
添加“Excel input”步骤,配置好需要读取的excel文件的信息,从上一步获取到的文件名流中获取文件名,并设置为excel文件名。
添加“Add constants”步骤,添加一个常值列,常量值设置为当前文件名,添加到excel数据中。
最后输出处理好的excel数据。
以下是Kettle代码的示例:
<?xml version="1.0" encoding="UTF-8"?>
<transformation>
<info>
<name>add filename to excel</name>
<description>add current filename as a column to excel</description>
<extended_description></extended_description>
<trans_version>9.1.0.0-324</trans_version>
<trans_type>Normal</trans_type>
<trans_status>0</trans_status>
<directory><project>/<subdir>/</directory>
<parameters></parameters>
</info>
<step>
<name>Get file names</name>
<type>GET_FILE_NAMES</type>
<description/>
<distribute>Y</distribute>
<custom_distribution/>
<copies>1</copies>
<partitioning>
<method>none</method>
<schema_name/>
<partitioning_per_slave>N</partitioning_per_slave>
<partitioning_fields/>
</partitioning>
<fields>
<field>
<name>filename</name>
<type>String</type>
<format/>
<currency/>
<decimal/>
<group/>
<nullif/>
<ifnull/>
<trim_type>none</trim_type>
<length>-1</length>
<precision>-1</precision>
<set_empty_string>N</set_empty_string>
</field>
</fields>
<file>
<name>Get file names</name>
<exclude_filemask/>
<file_required>N</file_required>
<include_subfolders>N</include_subfolders>
<field_include>N</field_include>
<type_include>N</type_include>
<last_modify_date_include>N</last_modify_date_include>
<uri_replace>N</uri_replace>
<filename_Field/>
<wildcard_Field/>
<exclude_wildcard_Field/>
<filecount_Field/>
<include_subfolder_Field/>
<last_modify_time_Field/>
<root_uri_Field/>
<extension_Field/>
<size_Field/>
<hidden_Field/>
<parent_folder_Field/>
<folder_Field/>
<short_filename_Field/>
</file>
<limit/>
</step>
<step>
<name>Excel input</name>
<type>EXCEL_INPUT</type>
<description/>
<distribute>Y</distribute>
<custom_distribution/>
<copies>1</copies>
<partitioning>
<method>none</method>
<schema_name/>
<partitioning_per_slave>N</partitioning_per_slave>
<partitioning_fields/>
</partitioning>
<fields>
<field>
<name>fieldname1</name>
<type>String</type>
<format/>
<currency/>
<decimal/>
<group/>
<nullif/>
<ifnull/>
<trim_type>none</trim_type>
<length>-1</length>
<precision>-1</precision>
<set_empty_string>N</set_empty_string>
</field>
<field>
<name>fieldname2</name>
<type>Number</type>
<format/>
<currency/>
<decimal/>
<group/>
<nullif/>
<ifnull/>
<trim_type>none</trim_type>
<length>-1</length>
<precision>-1</precision>
<set_empty_string>N</set_empty_string>
</field>
</fields>
<file>
<name>${filename}</name>
<sheetname>Sheet1</sheetname>
<startrow>1</startrow>
<type_excel>2007</type_excel>
<header>N</header>
<stop_on_empty>N</stop_on_empty>
</file>
<addl>
<last_column/>
<sheet_index_nr>0</sheet_index_nr>
<stop_on_empty>N</stop_on_empty>
<add_to_result_filenames>N</add_to_result_filenames>
</addl>
<limit/>
</step>
<step>
<name>Add constants</name>
<type>ADD_CONSTANTS</type>
<description/>
<distribute>Y</distribute>
<custom_distribution/>
<copies>1</copies>
<partitioning>
<method>none</method>
<schema_name/>
<partitioning_per_slave>N</partitioning_per_slave>
<partitioning_fields/>
</partitioning>
<fields>
<field>
<name>fieldname1</name>
<type>String</type>
<format/>
<currency/>
<decimal/>
<group/>
<nullif/>
<ifnull/>
<trim_type>none</trim_type>
<length>-1</length>
<precision>-1</precision>
<set_empty_string>N</set_empty_string>
<value>${filename}</value>
</field>
</fields>
</step>
<step>
<name>Excel output</name>
<type>EXCEL_OUTPUT</type>
<description/>
<distribute>Y</distribute>
<custom_distribution/>
<copies>1</copies>
<partitioning>
<method>none</method>
<schema_name/>
<partitioning_per_slave>N</partitioning_per_slave>
<partitioning_fields/>
</partitioning>
<fields>
<field>
<name>fieldname1</name>
<type>String</type>
<format/>
<currency/>
<decimal/>
<group/>
<nullif/>
<ifnull/>
<trim_type>none</trim_type>
<length>-1</length>
<precision>-1</precision>
<set_empty_string>N</set_empty_string>
</field>
<field>
<name>fieldname2</name>
<type>Number</type>
<format/>
<currency/>
<decimal/>
<group/>
<nullif/>
<ifnull/>
<trim_type>none</trim_type>
<length>-1</length>
<precision>-1</precision>
<set_empty_string>N</set_empty_string>
</field>
<field>
<name>filename</name>
<type>String</type>
<format/>
<currency/>
<decimal/>
<group/>
<nullif/>
<ifnull/>
<trim_type>none</trim_type>
<length>-1</length>
<precision>-1</precision>
<set_empty_string>N</set_empty_string>
</field>
</fields>
<file>
<name>newfile.xls</name>
<extention>xls</extention>
<do_not_open_newfile>N</do_not_open_newfile>
<createparent>N</createparent>
<append>N</append>
</file>
<addl>
<stepnr>0</stepnr>
<sheetname>Sheet1</sheetname>
<make_sheet_active>N</make_sheet_active>
<if_file_exists>do_nothing</if_file_exists>
<overwrite_y_n>N</overwrite_y_n>
<header>N</header>
</addl>
<limit/>
</step>
<hop>
<from>Get file names</from>
<to>Excel input</to>
<from_out>filename</from_out>
<to_in>filename</to_in>
<enabled>Y</enabled>
</hop>
<hop>
<from>Excel input</from>
<to>Add constants</to>
<from_out>fieldname1</from_out>
<to_in>fieldname1</to_in>
<enabled>Y</enabled>
</hop>
<hop>
<from>Add constants</from>
<to>Excel output</to>
<from_out>fieldname1</from_out>
<to_in>fieldname1</to_in>
<enabled>Y</enabled>
</hop>
</transformation>
注意:需要根据实际情况修改文件路径、字段名等参数。
如果我的回答解决了您的问题,请采纳!
Private Sub Workbook_Open()
Dim wbName As String
wbName = Replace(ThisWorkbook.Name, ".xlsx", "")
ActiveSheet.Cells(1, ActiveSheet.UsedRange.Columns.Count + 1).Value = wbName
End Sub
这段 VBA 代码会在每次打开 Excel 文件时自动运行,将当前文件名添加到最后一列的第一行。请注意,如果您的 Excel 文件中存在多个工作表,请确保将该 VBA 代码添加到每个工作表中。
在Kettle中获取文件名可以通过使用"Get Filenames"步骤来实现。此步骤可以获取指定文件夹中的所有文件的名称,你需要设置的是文件夹的路径和文件名的通配符(如*.xlsx)。
对于你要的需求,你可以先使用"Get Filenames"步骤来获取到所有需要处理的Excel文件的名称,然后再使用"Excel Input"步骤读取每个Excel文件。
在"Excel Input"步骤中,你可以通过添加自定义字段来获取到文件名。在"Spreadsheet Input"选项卡中,选择"添加"按钮,添加一个新字段,然后在"名称"选项中键入新字段的名称,如"filename"。在"类型"选项卡中,选择"文本"。在"值"选项卡中,选择通配符(*)并将其设置为你的Excel文件的名称字段。
完成上述步骤后,你就可以在每个Excel文件中添加一个名为"filename"的新字段,它将包含当前处理的Excel文件的名称。
如果你需要将这个值写入Excel文件中,你可以使用"Add Constants"步骤将其添加为常量列,或者使用"Calculator"步骤将其添加为新列。在"Calculator"步骤中,你可以使用字符串操作函数(如"concatenate")将"filename"字段中的值与其他字段值组合起来。
希望这些步骤会对你有帮助。
基于new bing的编写参考,有帮助记得采纳!:
你可以使用Kettle中的“Get File Names”步骤来获取一个文件夹中所有的文件名。获取到文件名后,你可以将文件名作为一个变量加入到数据流中。
然后,你可以使用“Excel Input”步骤来读取每个excel文件,并将前面获取到的文件名变量添加为一个新列。
最后,你可以使用“Excel Output”步骤将处理后的文件保存到指定的位置。请确保目标位置有写入权限。
在具体实现中,你可以按照以下步骤进行设置:
使用“Get File Names”步骤获取目标文件夹中的所有文件名,存储到一个变量中。
使用“Excel Input”步骤读取每个excel文件,并使用变量作为一个新列添加到数据流中。
使用“Excel Output”步骤将处理后的文件保存到指定位置。
将以上三个步骤按顺序连接起来,组成一个完整的Kettle转换。
在设置“Excel Input”步骤时,请注意使用正确的文件格式及文件路径,并正确配置“Header行数”、“Sheet名称”等参数。在设置“Excel Output”步骤时,请注意设置正确的保存路径、文件名及文件格式。
以下是Kettle中的转换XML代码示例,可实现你所需的功能:
<?xml version="1.0" encoding="UTF-8"?>
<transformation>
<info>
<name>add filename</name>
<description>增加文件名列</description>
<extended_description><![CDATA[在一个Excel表格中增加当前文件名列]]></extended_description>
<job_entry_log_level>Basic</job_entry_log_level>
<transformation_type>Normal</transformation_type>
<version>1.0</version>
<status>0</status>
</info>
<step>
<name>Get file names</name>
<type>GET_FILE_NAMES</type>
<description>获取目标文件夹中的所有文件名</description>
<distribute>Y</distribute>
<custom_distribution>false</custom_distribution>
<copies>1</copies>
<partitioning>
<method>none</method>
<schema_name />
</partitioning>
<fields>
<field>
<name>File path</name>
<type>File/Directory</type>
<description>源文件路径</description>
<format />
<currency />
<decimal />
<group />
<length>-1</length>
<precision>-1</precision>
<trim_type>none</trim_type>
<repeat />
<nullif />
<ifnull />
</field>
</fields>
<include_subfolders>N</include_subfolders>
<filemask>*.xlsx</filemask>
<exclude_filemask />
<file_required>N</file_required>
<include_hidden>N</include_hidden>
<use_regex>N</use_regex>
<dynamic_folder>N</dynamic_folder>
<wildcard>Y</wildcard>
<field_files_count />
<field_hidden />
<field_last_modified />
<field_path_filename />
<field_uri />
<limit>-1</limit>
<IsIgnoreEmptyFile>N</IsIgnoreEmptyFile>
<start_with_subfolder>N</start_with_subfolder>
<do_not_fail_if_no_files>Y</do_not_fail_if_no_files>
</step>
<step>
<name>Excel Input</name>
<type>Microsoft Excel Input</type>
<description>读取Excel表格</description>
<distribute>Y</distribute>
<custom_distribution>false</custom_distribution>
<copies>1</copies>
<partitioning>
<method>none</method>
<schema_name />
</partitioning>
<connection>Kettle File Repository</connection>
<file>PATH_TO_EXCEL_FILE</file>
<sheet>Sheet1</sheet>
<header>N</header>
<startrow>0</startrow>
<rowcount>0</rowcount>
<limit>-1</limit>
<encoding />
<add_to_result_filenames>N</add_to_result_filenames>
<is_file_template>N</is_file_template>
<spreadsheet_type>2007</spreadsheet_type>
<ignore_missing_worksheets>N</ignore_missing_worksheets>
<use_legacy_style>N</use_legacy_style>
<IgnoreEmptySheets>N</IgnoreEmptySheets>
<stop_on_empty>N</stop_on_empty>
<accept_filenames_from_previous>N</accept_filenames_from_previous>
<accept_field>A_FILENAME</accept_field>
<accept_field_type>Filename</accept_field_type>
<accept_stepname>Get file names</accept_stepname>
<expand_folders>N</expand_folders>
</step>
<step>
<name>Add constant</name>
<type>Constant</type>
<description>增加常量列</description>
<distribute>Y</distribute>
<custom_distribution>false</custom_distribution>
<copies>1</copies>
<partitioning>
<method>none</method>
<schema_name />
</partitioning>
<fields>
<field>
<name>FILENAME</name>
<type>String</type>
<description>当前文件名</description>
<format />
<currency />
<decimal />
<group />
<length>-1</length>
<precision>-1</precision>
<trim_type>none</trim_type>
<repeat />
<nullif />
<ifnull />
<value>PATH_TO_EXCEL_FILE</value>
<field_length>-1</field_length>
<field_precision>-1</field_precision>
<set_type_desc>N</set_type_desc>
</field>
</fields>
</step>
<step>
<name>Select values</name>
<type>SelectValues</type>
<description>选择需要的列</description>
<distribute>Y</distribute>
<custom_distribution>false</custom_distribution>
<copies>1</copies>
<partitioning>
<method>none</method>
<schema_name />
</partitioning>
<fields>
<field>
<name>A_FILENAME</name>
<type>Filename</type>
<description>文件名</description>
<format />
<currency />
<decimal />
<group />
<length>100</length>
<precision>-1</precision>
<trim_type>none</trim_type>
<repeat />
<nullif />
<ifnull />
<aggregation_type>none</aggregation_type>
<case_sensitive>N</case_sensitive>
<custom_name />
</field>
<field>
<name>NEW_COL</name>
<type>String</type>
<description>新增列</description>
<format />
<currency />
<decimal />
<group />
<length>-1</length>
<precision>-1</precision>
<trim_type>none</trim_type>
<repeat />
<nullif />
<ifnull />
<aggregation_type>none</aggregation_type>
<case_sensitive>N</case_sensitive>
<custom_name />
</field>
</fields>
<select_fields>N</select_fields>
<reset_row_number>N</reset_row_number>
<is_aggregate>N</is_aggregate>
<always_allocate_rowset>Y</always_allocate_rowset>
<database_meta />
<limit>-1</limit>
<lazy_conversion_active>N</lazy_conversion_active>
<cluster_schema />
</step>
<step>
<name>Excel Output</name>
<type>Microsoft Excel Output</type>
<description>保存为Excel表格</description>
<distribute>Y</distribute>
<custom_distribution>false</custom_distribution>
<copies>1</copies>
<partitioning>
<method>none</method>
<schema_name />
</partitioning>
<connection>Kettle File Repository</connection>
<append>N</append>
<create_parent_folder>Y</create_parent_folder>
<filename>PATH_TO_EXCEL_FILE</filename>
<xlsx_compress>N</xlsx_compress>
<create_sheets>N</create_sheets>
<field_type_list />
<split_every>0</split_every>
<add_to_result>Y</add_to_result>
<template_enabled>N</template_enabled>
<template_append>Y</template_append>
<sheetname>Sheet1</sheetname>
<leave_query_data>N</leave_query_data>
<password_encoding />
<encoding />
<header_enabled>N</header_enabled>
<footer_enabled>N</footer_enabled>
<header_font_name>Arial</header_font_name>
<header_font_size>10</header_font_size>
<header_font_color>000000</header_font_color>
<header_background_color>CCCCCC</header_background_color>
<header_row_height>-1</header_row_height>
<footer_font_name>Arial</footer_font_name>
<footer_font_size>10</footer_font_size>
<footer_font_color>000000</footer_font_color>
<footer_background_color>CCCCCC</footer_background_color>
<footer_row_height>-1</footer_row_height>
<row_font_name>Arial</row_font_name>
<row_font_size>10</row_font_size>
<row_font_color>FFFFFF</row_font_color>
<row_background_color>000000</row_background_color>
<row_alternative>N</row_alternative>
<row_alternative_font_name>Arial</row_alternative_font_name>
<row_alternative_font_size>10</row_alternative_font_size>
<row_alternative_font_color>FFFFFF</row_alternative_font_color>
<row_alternative_background_color>666666</row_alternative_background_color>
<row_height>-1</row_height>
<header_image_encoding>base64</header_image_encoding>
<header_image />
<comment_field />
<formula />
<shift_field />
<NoEmptyLines>N</NoEmptyLines>
<IgnorHeader>N</IgnorHeader>
<ProtectSheet>N</ProtectSheet>
<ProtectSheetPassword />
<AppendToExistingFile>N</AppendToExistingFile>
<AppendSeparator />
<AddToResultFilenames>Y</AddToResultFilenames>
<AppendFieldFilenames>N</AppendFieldFilenames>
<date_time_format />
</step>
<hop>
<from>Get file names</from>
<to>Excel Input</to>
<enabled>Y</enabled>
</hop>
<hop>
<from>Excel Input</from>
<to>Add constant</to>
<enabled>Y</enabled>
</hop>
<hop>
<from>Add constant</from>
<to>Select values</to>
<enabled>Y</enabled>
</hop>
<hop>
<from>Select values</from>
<to>Excel Output</to>
<enabled>Y</enabled>
</hop>
</transformation>
注意替换代码中的一些自定义参数,例如:PATH_TO_EXCEL_FILE,这些参数根据具体情况设置。
Excel输出:
Excel输出 ---- 输出.xls 文件
Microsoft Excel输出 ---- 输出.xlsx文件
任务:
从MySQL数据库的mysql库的user表读取数据插入到excel的.xls和.xlsx文件中
1.配置:
文件选择界面:
获取字段界面:
2.运行结果:
解决方式参考了https://blog.csdn.net/weixin_43329319/article/details/108348145
我的解决方式是
1.获取目录下的文件名
2.遍历上个转换的结果集并写入到新的excel
2.1