我正在运行一个批处理脚本,以将CSV转换为HTML文件,但是我需要使最后一行加粗,但是我尝试了其他方法使最后一行加粗,但是我无法将脚本转换为确定它是否是最后一行。
有什么想法吗?
@echo off
Title Convert csv file to HTML file
Rem Set what is your delimiters in your csv file like [,] [;] [:] [|] or [tab]
set "delims=,"
Rem Set the name of the CSV file into a variable
set "CSV_File=FinalReportFormatted.csv"
Rem Set the name of the HTML output file from CSV file into variable
for %%a in ("%CSV_File%") do set "HTML_File=%%~na.html"
if exist "%HTML_File%" del /f /q "%HTML_File%"
Call :CreateHTMLtable "%CSV_File%" "%HTML_File%"
exit /b
::******************************************************************************************************
:CreateHTMLTable <inputfile> <outputfile>
setlocal
(
echo ^<!DOCTYPE HTML PUBLIC
echo "-//W3C//DTD HTML 4.01 Transitional//EN"
echo "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"^>
echo ^<HTML^>
echo ^<HEAD^>
echo ^<META HTTP-EQUIV="Content-Type"
echo CONTENT="text/html; charset=utf-8"^>
echo ^</HEAD^>
echo ^<BODY^>
echo ^<style type="text/css"^>
echo .tftable {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #bcaf91;border-collapse: collapse;}
echo .tftable th {font-size:12px;background-color:#fff;border-width: 1px;padding: 8px;border-style: solid;border-color: #bcaf91;text-align:center;}
echo .tftable tr {background-color:#ffffff;}
echo .tftable td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #bcaf91; text-align:center;}
echo .tftable tr:hover {background-color:#ffffff;}
echo ^</style^>
echo ^<p^>
echo ^<Dear All,^>
echo ^</p^>
echo ^<center^>^<table class="tftable" border="1"^>
)>%2
setlocal enabledelayedexpansion
for /F "delims=" %%A in ('Type "%~1"') do (
set "var=%%A"
set "var=!var:&=&!"
set "var=!var:<=<!"
set "var=!var:>=>!"
set "var=!var:%delims%=</td><td>!"
echo ^<tr^>^<td^>!var!^</td^>^</tr^>
)>>%2
(
echo ^</table^>^</center^>
echo ^</BODY^>
echo ^</HTML^>
)>>%2
endlocal
::******************************************************************************************************
答案 0 :(得分:1)
我同意Compo的观点,即使用PowerShell是更具前瞻性的方法,
但由于该批处理似乎效果很好,请实施monotero的建议:
find /V /C "" <"%~1"
来计数.csv文件的行Lines
:CreateHTMLTable
的中间部分<B>
,</B>
标签。for /f "delims=" %%A in ('find.exe /V /C "" ^<"%~1"') Do Set Lines=%%A
setlocal enabledelayedexpansion
for /F "delims=" %%A in ('Type "%~1"') do (
set "var=%%A"
set "var=!var:&=&!"
set "var=!var:<=<!"
set "var=!var:>=>!"
Set /A Lines-=1
if !Lines! gtr 0 (
set "var=!var:%delims%=</td><td>!"
echo ^<tr^>^<td^>!var!^</td^>^</tr^>
) else (
set "var=!var:%delims%=</B></td><td><B>!"
echo ^<tr^>^<td^>^<B^>!var!^</B^>^</td^>^</tr^>
)
)>>%2