我有一个现有的word文档,它有一个格式化的邮件合并字段(private void alert(String msg){
//this doesn't print to console, doesn't show alert
PrintWriter out = new PrintWriter(System.out);
out.println("<script type=\"text/javascript\">");
out.println("alert(" + msg + ");");
out.println("</script>");
//This prints to console only
/*System.out.println("<script type=\"text/javascript\">");
System.out.println("alert(" + msg + ");");
System.out.println("</script>");*/
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// gets values of text fields
String name = request.getParameter("name");
InputStream inputStream = null; // input stream of the upload file
// obtains the upload file part in this multipart request
Part filePart = request.getPart("file");
if (filePart != null) {
inputStream = filePart.getInputStream();
}
Connection conn = null; // connection to the database
try {
// connects to the database
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
// constructs SQL statement
String sql = "INSERT INTO table(name, image) values (?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, name);
if (inputStream != null) {
statement.setBlob(2, inputStream);
}
// sends the statement to the database server
int row = statement.executeUpdate();
if (row > 0) {
//This ends up appearing on my desktop instead of inside the browser.
//JOptionPane.showMessageDialog(null, "Input successful");
//**THIS is the alert that doesn't show up**
alert("Input successful");
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
// closes the database connection
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
response.sendRedirect("home.jsp");
}
}
)。
当我从C#传递一个字符串(比如说(01/01/2016))来进行邮件合并时,代码字段.Select()选择整个合并字段并用我传递的字符串替换它,我失去了格式。
我该如何防止这种情况?
{ MERGEFIELD Payment_Date \@ "MMMM d,yyyy" }
这是我用来进行邮件合并的代码。
答案 0 :(得分:0)
您必须从字段中提取格式字符串并将其转换为可以正确格式化C#字符串的内容,或者(更简单的IMO)将字段替换为保留格式字符串的字段并让Word执行格式设置您。
例如,您可以将其替换为
{ QUOTE "2016-12-30" \@ "MMMM d,yyyy" }
(我建议您以YYYY-MM-DD格式插入日期字符串,因为我相信在这种情况下Word总是正确地解释日期和月份。)
(注意,您不能只用{}插入文本 - 您必须插入一个字段。然后,如果您只想要结果,您可以确保该字段已更新,然后取消链接字段,只需离开结果)。
还有一种情况 - 如果MERGEFIELD字段没有日期/时间格式切换怎么办?在这种情况下,您需要强加一种格式(我认为不可能发现文档作者的意图)。