回复 60楼 TonyDeng
帮鼓鼓劲
你对照下面的效果图和模板,自然知道模板怎么写,不详细说明了:
程序代码:
CLEAR ALL
CLEAR
Main()
CLEAR ALL
RETURN
PROCEDURE Main()
LOCAL lcFileName, loExcel, lnRow, lnCol, lnIndex, lnRecIndex
LOCAL lnFile, laString[1], lcBuffer
LOCAL lnMaxRows, lnMaxCols
lnMaxRows = 10
lnMaxCols = 4
lcFileName = "工资条.txt"
IF !FILE(lcFileName)
MESSAGEBOX("文件" + lcFileName + "不存在!", 16, "")
RETURN
ENDIF
lnFile = FOPEN(lcFileName)
IF lnFile == -1
MESSAGEBOX("文件" + lcFileName + "打开失败!", 16, "")
RETURN
ENDIF
lnIndex = 0
DO WHILE !FEOF(lnFile)
lnIndex = lnIndex + 1
DIMENSION laString[lnIndex]
laString[lnIndex] = ALLTRIM(FGETS(lnFile))
ENDDO
FCLOSE(lnFile)
IF !USED("工资")
USE test_prn ALIAS 工资 IN 0
ENDIF
lcFileName = Application.DefaultFilePath + "\工资条"
IF FILE(lcFileName + ".xls")
loFileName = lcFileName + ".xls"
DELETE FILE (lcFileName)
ELSE
IF FILE(lcFileName + ".xlsx")
lcFileName = lcFileName + ".xlsx"
DELETE FILE (lcFileName)
ENDIF
ENDIF
WAIT "正在启动 Microsoft Excel Application,请稍候……" WINDOW NOWAIT
loExcel = CREATEOBJECT("Excel.Application")
WAIT "正在生成工资条,请稍候……" WINDOW NOWAIT
WITH loExcel
.SheetsInNewWorkbook = 1
.Workbooks.Add
.WorkSheets(1).Activate
lnRecIndex = 0
SELECT 工资
SCAN ALL
lnRecIndex = lnRecIndex + 1
lnRow = 1 + (lnRecIndex - 1) * lnMaxRows
IF lnRecIndex > 1
.ActiveSheet.Rows(1 + (lnRecIndex - 1) * lnMaxRows).PageBreak = 1
ENDIF
FOR lnIndex = 1 TO ALEN(laString, 1)
lnCol = 2
lcBuffer = laString[lnIndex]
DO WHILE !EMPTY(lcBuffer)
LOCAL lcCaption, lcExpression, llBold
Get_Expression(Get_Unit(@lcBuffer), @lcCaption, @lcExpression, @llBold)
IF EMPTY(lcCaption) .AND. EMPTY(lcExpression)
lnCol = lnCol + 1
ENDIF
IF !EMPTY(lcExpression) .AND. !Empty(EVALUATE(lcExpression))
IF !EMPTY(lcCaption)
WITH .Cells(lnRow + lnIndex, lnCol)
.Value = lcCaption
.Font.Bold = llBold
.HorizontalAlignment = 3
ENDWITH
WITH .Cells(lnRow + lnIndex, lnCol + 1)
.Value = EVALUATE(lcExpression)
.Font.Bold = llBold
.HorizontalAlignment = 3
ENDWITH
lnCol = lnCol + 2
ELSE
WITH .Cells(lnRow + lnIndex, lnCol)
.Value = EVALUATE(lcExpression)
.Font.Bold = llBold
.HorizontalAlignment = 3
ENDWITH
lnCol = lnCol + 1
ENDIF
IF lnCol > 2 * lnMaxCols
lnRow = lnRow + 1
lnCol = 2
ENDIF
ENDIF
ENDDO
NEXT
ENDSCAN
.ActiveWorkbook.SaveAs(lcFileName)
.Quit
ENDWITH
RELEASE loExcel
WAIT "工资条已生成" WINDOW
ENDPROC
FUNCTION Get_Unit(tcString)
LOCAL lnPos, lcUnit
lcUnit = ""
IF !EMPTY(tcString)
lnPos = AT(",", tcString)
IF lnPos > 0
lcUnit = ALLTRIM(LEFT(tcString, lnPos - 1))
tcString = SUBSTR(tcString, lnPos + 1)
ELSE
lcUnit = ALLTRIM(tcString)
tcString = ""
ENDIF
ENDIF
RETURN lcUnit
ENDFUNC
PROCEDURE Get_Expression(tcString, tcCaption, tcExpression, tlBold)
LOCAL lnPos1, lnPos2
tcCaption = ""
tcExpression = ""
tlBold = ("<B>" $ tcString)
lnPos1 = AT("[", tcString)
lnPos2 = AT("]", tcString)
IF (lnPos1 > 0) .AND. (lnPos2 > 0)
tcCaption = ALLTRIM(SUBSTR(tcString, lnPos1 + 1, lnPos2 - lnPos1 - 1))
ENDIF
lnPos1 = AT("{", tcString)
lnPos2 = AT("}", tcString)
IF (lnPos1 > 0) .AND. (lnPos2 > 0)
tcExpression = ALLTRIM(SUBSTR(tcString, lnPos1 + 1, lnPos2 - lnPos1 - 1))
ENDIF
ENDPROC

程序代码:
CLEAR ALL
CLEAR
Main()
CLEAR ALL
RETURN
PROCEDURE Main()
LOCAL lcFileName, loExcel, lnRow, lnCol, lnIndex, lnRecIndex
LOCAL lnFile, laString[1], lcBuffer
LOCAL lnMaxRows, lnMaxCols
lnMaxRows = 10
lnMaxCols = 4
lcFileName = "工资条.txt"
IF !FILE(lcFileName)
MESSAGEBOX("文件" + lcFileName + "不存在!", 16, "")
RETURN
ENDIF
lnFile = FOPEN(lcFileName)
IF lnFile == -1
MESSAGEBOX("文件" + lcFileName + "打开失败!", 16, "")
RETURN
ENDIF
lnIndex = 0
DO WHILE !FEOF(lnFile)
lnIndex = lnIndex + 1
DIMENSION laString[lnIndex]
laString[lnIndex] = ALLTRIM(FGETS(lnFile))
ENDDO
FCLOSE(lnFile)
IF !USED("工资")
USE test_prn ALIAS 工资 IN 0
ENDIF
lcFileName = Application.DefaultFilePath + "\工资条"
IF FILE(lcFileName + ".xls")
loFileName = lcFileName + ".xls"
DELETE FILE (lcFileName)
ELSE
IF FILE(lcFileName + ".xlsx")
lcFileName = lcFileName + ".xlsx"
DELETE FILE (lcFileName)
ENDIF
ENDIF
WAIT "正在启动 Microsoft Excel Application,请稍候……" WINDOW NOWAIT
loExcel = CREATEOBJECT("Excel.Application")
WAIT "正在生成工资条,请稍候……" WINDOW NOWAIT
WITH loExcel
.SheetsInNewWorkbook = 1
.Workbooks.Add
.WorkSheets(1).Activate
lnRecIndex = 0
SELECT 工资
SCAN ALL
lnRecIndex = lnRecIndex + 1
lnRow = 1 + (lnRecIndex - 1) * lnMaxRows
IF lnRecIndex > 1
.ActiveSheet.Rows(1 + (lnRecIndex - 1) * lnMaxRows).PageBreak = 1
ENDIF
FOR lnIndex = 1 TO ALEN(laString, 1)
lnCol = 2
lcBuffer = laString[lnIndex]
DO WHILE !EMPTY(lcBuffer)
LOCAL lcCaption, lcExpression, llBold
Get_Expression(Get_Unit(@lcBuffer), @lcCaption, @lcExpression, @llBold)
IF lnCol > (2 * lnMaxCols)
lnRow = lnRow + 1
lnCol = 2
ENDIF
IF EMPTY(lcCaption) .AND. EMPTY(lcExpression)
lnCol = lnCol + 1
ENDIF
IF !EMPTY(lcExpression) .AND. !Empty(EVALUATE(lcExpression))
IF !EMPTY(lcCaption)
WITH .Cells(lnRow + lnIndex, lnCol)
.Value = lcCaption
IF llBold
.Font.Bold = .T.
ENDIF
.HorizontalAlignment = 3
ENDWITH
WITH .Cells(lnRow + lnIndex, lnCol + 1)
.Value = TRANSFORM(EVALUATE(lcExpression))
IF llBold
.Font.Bold = .T.
ENDIF
.HorizontalAlignment = 3
ENDWITH
lnCol = lnCol + 2
ELSE
WITH .Cells(lnRow + lnIndex, lnCol)
.Value = TRANSFORM(EVALUATE(lcExpression))
IF llBold
.Font.Bold = .T.
ENDIF
.HorizontalAlignment = 3
ENDWITH
lnCol = lnCol + 1
ENDIF
ENDIF
ENDDO
NEXT
ENDSCAN
.ActiveWorkbook.SaveAs(lcFileName)
.Quit
ENDWITH
RELEASE loExcel
WAIT "工资条已生成" WINDOW
ENDPROC
FUNCTION Get_Unit(tcString)
LOCAL lnPos, lcUnit
lcUnit = ""
IF !EMPTY(tcString)
lnPos = AT(",", tcString)
IF lnPos > 0
lcUnit = ALLTRIM(LEFT(tcString, lnPos - 1))
tcString = SUBSTR(tcString, lnPos + 1)
ELSE
lcUnit = ALLTRIM(tcString)
tcString = ""
ENDIF
ENDIF
RETURN lcUnit
ENDFUNC
PROCEDURE Get_Expression(tcString, tcCaption, tcExpression, tlBold)
LOCAL lnPos1, lnPos2
tcCaption = ""
tcExpression = ""
tlBold = ("<B>" $ tcString)
lnPos1 = AT("[", tcString)
lnPos2 = AT("]", tcString)
IF (lnPos1 > 0) .AND. (lnPos2 > 0)
tcCaption = ALLTRIM(SUBSTR(tcString, lnPos1 + 1, lnPos2 - lnPos1 - 1))
ENDIF
lnPos1 = AT("{", tcString)
lnPos2 = AT("}", tcString)
IF (lnPos1 > 0) .AND. (lnPos2 > 0)
tcExpression = ALLTRIM(SUBSTR(tcString, lnPos1 + 1, lnPos2 - lnPos1 - 1))
ENDIF
ENDPROC
